PHP | SQL syntax error when inserting array
- by Philip
Hi guys,
I am having some trouble inserting an array into the sql database.
my error is as follows:
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
My query var_dump is:
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)"
My question is how can i add an empty value to id as it is the primary key and not news_id
my insert function looks like this:
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));
}
}
Note: arrays are not my strong point so i may be using them in-correctly!