我在将数组插入sql数据库时遇到了一些问题。PHP |插入数组时出现SQL语法错误
我的错误如下:
Unable to add : 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 '06:45:23,i want to leave a comment)' at line 1
我查询的var_dump是:
string(136) "INSERT INTO news_comments (news_id,comment_by,comment_date,comment) VALUES (17263,Philip,2010-05-11 06:45:23,i want to leave a comment)"
我的问题是我怎么能一个空值添加到ID,因为它是主键,而不是news_id
我插入功能如下:
function insertQuery($tbl, &$data)
{
global $mysqli;
$_SESSION['errors'] = array();
require_once '../config/mysqli.php';
$query = "INSERT INTO $tbl (".implode(',',array_keys($data)).") VALUES (".implode(',',array_values($data)).")";
var_dump($query);
if($result = mysqli_query($mysqli, $query))
{
//$id = mysqli_insert_id($mysqli);
print 'Very well done sir!';
}
else
{
array_push($_SESSION['errors'], 'Unable to add : ' . mysqli_error($mysqli));
}
}
注意:数组不是我的强项,所以我可能正确使用它们!
+1对于提及转义报价 – Simon 2010-05-11 18:27:15