简体   繁体   English

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

[英]INSERT query not working even though the syntax is correct

My insert query isnt adding anything to the database, i checked my syntax and it is correct 我的插入查询未向数据库添加任何内容,我检查了语法,这是正确的

$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)");

I even input the insert query in through phpmyadmin with values but still it does not insert anything into the table, explain this please and be simple, i am a beginner.I am using WAMPserver for this 我什至通过phpmyadmin输入带有值的插入查询,但仍然没有在表中插入任何内容,请简单地说明一下,我是一个初学者。为此,我正在使用WAMPserver

Try putting quotes around the values: 尝试将引号放在值周围:

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

You must use quote for the string insertion, 您必须在双引号中使用引号,

$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')");

if this does not works, than echo your query, 如果这不起作用,则回显您的查询,

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

and run in your phpmyadmin 并在您的phpmyadmin中运行

No, it's not correct. 不,这是不正确的。 You are vulnerable to SQL injection attacks , and haven't even bothered to check if the SQL is TRULY correct. 你很容易受到SQL注入式攻击 ,并且甚至还没有费心去检查SQL 确实是正确的。

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

Given that is probably this product sucks , your query is actually: 鉴于this product sucks可能this product sucks ,您的查询实际上是:

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

which is an outright SQL syntax error. 这是一个完全的SQL语法错误。

Never EVER assume that your query will succeed. 永远不要假设你的查询会成功。 Assuming everything will fail, check for that failure, and treat success as a pleasant surprise: 假设一切都会失败,请检查该失败,并将成功视为惊喜:

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

is the absolute bare minimum you should have, especially while developing/debugging 是您应该拥有的绝对最低要求,尤其是在开发/调试时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM