即使语法正确,INSERT查询也不起作用

时间:2013-12-24 15:51:05

标签: php mysql sql

我的插入查询没有向数据库添加任何内容,我检查了我的语法并且它是正确的

$id = $_SESSION['id'];//the current book id
$name = $_SESSION['username'] ;
$rating = $_POST['rate'];
$review = $_POST['review'];
echo "<br>";
echo "$name"." -$rating"." -$review";

$insert = mysqli_query($con,"INSERT INTO rating (bid,rating,review) 
VALUES ($id,$rating,$review)");

我甚至通过phpmyadmin输入插入查询的值,但它仍然没有在表中插入任何内容,请解释这个并且很简单,我是初学者。我正在使用WAMPserver

3 个答案:

答案 0 :(得分:2)

尝试在值周围加上引号:

INSERT INTO rating (bid,rating,review) 
    VALUES ('$id', '$rating',' $review')

答案 1 :(得分:1)

您必须使用引号进行字符串插入,

$id = $_SESSION['id'];//the current book id
$name = $_SESSION['username'] ;
$rating = $_POST['rate'];
$review = $_POST['review'];
echo "<br>";
echo "$name"." -$rating"." -$review";

$insert = mysqli_query($con,"INSERT INTO rating (bid,rating,review) 
VALUES ('$id','$rating','$review')");

如果这不起作用,则回显您的查询,

echo "INSERT INTO rating (bid,rating,review) 
VALUES ('$id','$rating','$review')"; 

并在你的phpmyadmin中运行

答案 2 :(得分:0)

不,这不对。您很容易受到SQL injection attacks的攻击,甚至不愿意检查SQL是否 TRULY 正确。

$insert = mysqli_query($con,"INSERT [snip] VALUES ($id,$rating,$review)");
                                                               ^------^--

鉴于这可能是this product sucks,您的查询实际上是:

INSERT INTO ... VALUES(foo, bar, this product sucks)

这是一个彻头彻尾的SQL语法错误。

从不 EVER 假设您的查询会成功。假设一切都会失败,检查失败,并将成功视为一个惊喜:

$result = mysqli_query($con, $sql) or die(mysqli_error($con));
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

是你应该拥有的绝对最低限度,特别是在开发/调试时

相关问题