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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动