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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题