Insert Array field into mysql in php -
when insert type of array values directly mysql database, got error this
#1064 - have error in sql syntax; check manual corresponds mysql server version right syntax use near ':23:09z, 36840bd430637, success, 85.0, 11457922, 10.02, usd, x, m, 59106737wv831' @ line 1
and myquery is
insert `transaction`(timestamp, correlationid, ack, version, build, amt, currencycode, avscode, cvv2match, transactionid) values (2014-06-26t02:23:09z, 36840bd430637, success, 85.0, 11457922, 10.02, usd, x, m, 59106737wv831451u)
mycode is
$columns = implode(", ",array_keys($result_array)); $escaped_values = array_map('mysql_real_escape_string', array_values($result_array)); $values = implode(", ", $escaped_values); echo $sql = "insert `transaction`($columns) values ($values)"; $res =mysql_query($sql);
what changes can do?
first of all, should escape column names (not required):
$cols = join(',', array_map(function($name) { return '`' . str_replace('`', '``', $name) . '`'; }, array_keys($result_array));
then, keeping in mind mysql_real_escape_string()
doesn't add quoted enclosures:
$vals = join(',', array_map(function($value) { return "'" . mysql_real_escape_string($value) . "'"; }, $result_array); $sql = "insert `transaction` ($cols) values ($vals)";
lastly, using mysql_
functions deprecated , should move onto using either pdo or mysqli.
Comments
Post a Comment