php mysqli语法错误

时间:2013-09-21 05:53:57

标签: php mysql

我的代码出了什么问题?我确定$_POST['item']具有有效值

<?php

$data = $_POST['item'];

$conn = mysqli_connect("localhost","root","");
mysqli_select_db($conn, "ajaxexample");

$q = INSERT INTO user (userList) VALUES ('$data');
if(mysqli_query($conn, $q)){

echo 1;

}

?>

3 个答案:

答案 0 :(得分:2)

INSERT INTO user (userList) VALUES ('$data');放在双引号中。

例如:

$q = "INSERT INTO user (userList) VALUES ('$data')";

答案 1 :(得分:1)

PHP字符串文字必须在引号中。

要通过仅更改一行来解决此问题:

$q = "INSERT INTO user (userList) VALUES ('" . mysqli_real_escape_string($data . "')";

答案 2 :(得分:0)

<?php

$data = $_POST['item'];

$conn = mysqli_connect("localhost","root","", "ajaxexample");

$q = INSERT INTO user (userList) VALUES ('$data');
if(mysqli_query($conn, $q)){

echo 1;

}

?>

Not mysqli_select_db