为什么我的 mySQL 插入不起作用


Why is my mySQL insert not working?

一些信息是通过PHP表单提交到此页面的,但是当我发布它时,我收到此错误...

Error: 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 '-code) VALUES ('','$title','$description','$post','$author','11th September 20' at line 1

有人知道这是为什么吗?

<?php
    $description = addslashes($_POST[description]);
    $post = addslashes($_POST[post]);
    $title = addslashes($_POST[title]);
    $date_posted = date("jS F Y");
    $con=mysqli_connect("localhost","root","********","**********");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql="INSERT INTO posts 
            (id, title, description, 
            post, author, date_posted, 
            category, image-code)
        VALUES
            ('','$_POST[title]','$_POST[description]',
            '$_POST[post]','$_POST[author]','$date_posted',
            '$_POST[category]','$image')";
    if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }
    echo "Draft Successfully Submitted!";
    mysqli_close($con);
    header("Location:queue.php");
?>

如果要在列名称中使用破折号,则必须将它们括在刻度中:

`image-code`

否则,它看起来像减法运算符。

上面的代码部分没有定义$image,因此它将作为文字传递给MySQL。