INSERT INTO语句不会将数据发布到表

时间:2014-10-03 17:41:09

标签: php mysql

我正在尝试将数据发布到我的数据库中的表中,但是没有关于未发布数据的错误消息。我已经在phpmyadmin中将数据插入到表中,并且这些数据在循环时打印出结果,但数据不会发布到表中。

<!-- form to take input-->
    <form name='form1' method='post'>
    Name:
    <input type='text' name='Name' id='name' /> <br />
    Comment:
    <input type='text' name='Comment' id='comment' /> <br />
    <input type="submit" name='submit' value="Submit" id='submit'>
    </form>
    <!-- start php-->
    <?php
            if(isset($_POST['submit']))
            {
                $name = $_POST['Name'];
                $comment = $_POST['Comment'];
            }

            $con = mysqli_connect("localhost", "kodie", "hill1124", "comments");
            if(mysqli_connect_errno())
            {
                echo "Failed to connect to MySql: ". mysqli_connect_error();
            }

            mysqli_query($con, "INSERT INTO commenttable VALUES ('$name','$comment',NOW()");
            $query = "SELECT * FROM commenttable"; 
            $result = mysqli_query($con, $query);
            $hash = $result;
            echo "<table>";
            if($hash = NULL)
            {
                echo "null";
            }
            while($row = mysqli_fetch_array($result))
            {   
            echo "<tr><td>" . $row['Name'] . "</td><td>" .  $row['comment'] . "</td><td>" . $row['timestamp'] . "</td></tr>";  //$row['index'] the index here is a field name
            }

            echo "</table>"; 

            mysqli_close($con); 

    ?>

我不确定为什么它不会发布,我不认为它是权限但我不熟悉使用mysql并且不理解为什么语句编译没有错误但是没有&#39;实际上把数据放在桌子上。

感谢任何帮助。

5 个答案:

答案 0 :(得分:4)

为了在数据库中INSERT数据,您需要调整插入查询。 你现在拥有什么:

mysqli_query($con, "INSERT INTO commenttable VALUES ('$name','$comment',NOW()");

应该是

mysqli_query($con, "INSERT INTO commenttable VALUES ('$name', '$comment', NOW())");

您还应考虑使用mysqli_real_escape_string来阻止SQL-injection

所以:

$name = $_POST['Name'];
$comment = $_POST['Comment'];

变为:

$name = mysqli_real_escape_string($con, $_POST['Name']);
$comment = mysqli_real_escape_string($con, $_POST['Comment']);

您还可以查看以下内容:

<强> 更新

<!-- form to take input-->
<form action="" name="form1" method="post">
    Name:
    <input type="text" name="Name" id="name"> <br>
    Comment:
    <input type="text" name="Comment" id="comment"> <br>
    <input type="submit" name='submit' value="Submit" id="submit">
</form>
<!-- start php-->

<?php
if($_POST) {
    $con = mysqli_connect("localhost", "kodie", "hill1124", "comments");

    $name = mysqli_real_escape_string($con, trim($_POST['Name']));
    $comment = mysqli_real_escape_string($con, trim($_POST['Comment']));

    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySql: " . mysqli_connect_error();
    }

    if (!empty($name) && !empty($comment)) {
        $query = mysqli_query($con, "INSERT INTO commenttable VALUES ('$name','$comment',NOW())");

        // Check if the query succeeded
        if (mysqli_affected_rows($con)) {
            $query = "SELECT * FROM commenttable";
            $result = mysqli_query($con, $query);
            $hash = $result;
            echo "<table>";
            }
        } else {
            echo 'Something went wrong: '. mysqli_error($con); // Echo the error (You could replace echo with die())
        }
    }

    if ($hash = NULL) {
        echo "null";
    }

    while ($row = mysqli_fetch_array($result)) {
        echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['comment'] . "</td><td>" . $row['timestamp'] . "</td></tr>";  //$row['index'] the index here is a field name
    }

    echo "</table>";

    mysqli_close($con); // this is not necessary
}
?>

答案 1 :(得分:0)

请参阅第mysqli_query($con, "INSERT INTO commenttable VALUES ('$name','$comment',NOW()");

您尚未关闭mysqli_query()

必须是mysqli_query($con, "INSERT INTO commenttable VALUES ('$name','$comment',NOW()"));

答案 2 :(得分:0)

您在)声明的末尾错过了结束insert

mysqli_query
   ($con, 
    "INSERT INTO commenttable VALUES ('$name','$comment',NOW())");
    //                                               This one ^

答案 3 :(得分:0)

确保用户“kodie”有privileges进行INSERT

答案 4 :(得分:-1)

中缺少括号
mysqli_query($con, "INSERT INTO commenttable VALUES ('$name','$comment',NOW()");

应该是

mysqli_query($con, "INSERT INTO commenttable VALUES ('$name','$comment',NOW()")); // missing )