dongquexi1990 2015-11-17 15:06
浏览 43
已采纳

“字段列表”中的未知列[重复]

This question already has an answer here:

hello this is an assignment from our class and i have this problem inserting a data in my database called userdb. i have a form that accepts 3 fields: Name, Subject and Message. And in my localhost/phpmyadmin i have a table called data which has 5 fields : userID (auto increment) (int) , name (varchar), subject (varchar), message (text) and Timestamp (date) . And when I submit data to my form, this error pops up : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'john!, 15-11-17)' at line 1

    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">

        Name : &nbsp&nbsp&nbsp<input type="text" name="name" /> <br>
        Subject : &nbsp<input type="text" name="subject" > <br> Message :  
        <br> <textarea name="message" type="text" style="width:200px; height:100px"></textarea> <br>
        <input type="submit" name="btnSubmit" />
    </form>

</div>


<?php
if($_POST) {
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $date = date("y-m-d");


    $user = "jarvs";
    $pass = "strumandplay";
    $db="userdb";
    $conn = mysqli_connect("localhost", $user, $pass, $db);

    if(!mysqli_connect()){
        die("failed to connect to server " . mysqli_connect_error());
    }else{
        echo "connected" . "<br>";
    }

    $query = "INSERT INTO data (userID, Subject, Message, Timestamp) VALUES (1, $subject, $message, $date)";


    if (mysqli_query($conn, $query)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $query . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);

}

?>

thanks for answering.

</div>
  • 写回答

1条回答 默认 最新

  • doukong9982 2015-11-17 15:11
    关注

    echo your $query var to see what's wrong. You will find that strings are not quoted:

    Do it like this:

    $query = "INSERT INTO data (userID, Subject, Message, Timestamp) VALUES (1, '$subject', '$message', '$date')";
    

    Next step will be to escape the $subject, $message and $date to not crash the script when they contain a quote '.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大