将数据插入SQL数据库不起作用,但表示已有

时间:2016-01-16 07:42:52

标签: php mysql pdo

所以我现在的网站没有正确插入数据。 我确实有id和sub_date(它提交的日期),但这些是自动的我会想到所以不需要为它和东西创建变量?如我错了请纠正我?

PHP代码:

<?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代码:

<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>

1 个答案:

答案 0 :(得分:0)

$ _ POST ['statusAdd']未定义,在您的表单中,它实际上已命名为status。所以只需更改以下内容。

改变这个:

$statusAdd = $_POST['statusAdd'];

对此:

$statusAdd = $_POST['status'];

然后,对于插入查询,请确保更改此项,在这种情况下,您不需要为id指定null:

VALUES(NULL, :id, etc

对此:

VALUES(:id, etc