douqi5079 2016-01-16 07:42
浏览 30
已采纳

将数据插入SQL数据库不起作用但表示它有[重复]

So the website I have at the minute isn't inserting the data correctly. I do have id and sub_date (date it got submitted) but these are automatic I would of thought so don't need to create variables for it and stuff? Correct me if im wrong?

PHP Code:

<?php
if (isset($_POST['addBtn']))
{
    $m_titleAdd = $_POST['m_titleAdd'];
    $authorAdd = $_POST['authorAdd'];
    $statusAdd = $_POST['statusAdd'];
    $tutorialAdd = $_POST['tutorialAdd'];
    if (empty($errors))
    {
        $SQLinsert = $odb -> prepare("INSERT INTO `methods` VALUES(NULL, :id, :m_title, :sub_date, :status, :author, :tutorial)");
        $SQLinsert -> execute(array(':id' => null, ':m_title' => $m_titleAdd, ':sub_date' => null, ':status' => $statusAdd, ':author' => $authorAdd, ':tutorial' => $tutorialAdd));
        echo '<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong>Method has been added</p></div>';
    }
    else
    {
        echo '<div class="nNote nFailure hideit"><p><strong>ERROR:</strong><br />';
        foreach($errors as $error)
        {
            echo '-'.$error.'<br />';
        }
        echo '</div>';
    }
}
?>

HTML Code:

<form action="" method="POST">
        <fieldset>
                <div class="formRow">
                    <label>Method Title:</label>
                    <div class="formRight"><input type="text" name="m_titleAdd" class="form-control round" style="width:300px;" /></div>
                    <div class="clear"></div>
                </div>
                <div class="formRow">
                    <label>Tutorial:</label>
                    <div class="formRight"><textarea style="resize:none;" rows="3" cols="15" name="tutorialAdd" class="autoGrow form-control round"></textarea></div>
                    <div class="clear"></div>
                </div>
                <div class="formRow">
                    <label>Author:</label>
                    <div class="formRight"><input type="text" name="authorAdd" class="form-control round" style="width:300px;" /></div>
                    <div class="clear"></div>
                </div>
                <div class="control-group">
                <label class="control-label">Type:</label>
                <div class="controls">
                <select class="btn btn-default dropdown-toggle" name="status" tabindex="1">
                <option value="Working" />Working
                <option value="Plausible" />Plausible
                <option value="Not Working" />Not Working
                </select>
                </div>
                </div><br>
                <div class="formRow">
                    <div style="width:100px;"><input type="submit" value="Add" name="addBtn" class="btn btn-primary btn-block btn-round" /></div>
                     <div class="clear"></div>
                </div><br>
        </fieldset>
    </form>
</div>
  • 写回答

1条回答 默认 最新

  • douhanxujiuji6098 2016-01-16 08:49
    关注

    $_POST['statusAdd'] is undefined and in your form it is actually named status. So just change the following.

    Change this:

    $statusAdd = $_POST['statusAdd'];
    

    To this:

    $statusAdd = $_POST['status'];
    

    Then for your insert query make sure you change this, you don't need to specify null for the id in this case:

    VALUES(NULL, :id, etc
    

    To this:

    VALUES(:id, etc
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)