简体   繁体   English

PHP将数组值插入MySQL数据库

[英]Php insert array value into mysql database

Please advice where i am doing wrong? 请指教我在哪里做错了?

$db     = new mysqli("localhost","root","","saad");
$prep = array();
foreach($data_array as $k => $v ) {
    $prep[':'.$k] = $v;
}
$sth = $db->prepare("INSERT INTO records ( " . implode(', ',array_keys($data_array)) . ") VALUES (" . implode(', ',array_keys($prep)) . ")");
$res = $sth->execute($prep);

something i am not doing right 我做的不好

Consider moving the query string into a separate variable: 考虑将查询字符串移动到单独的变量中:

$sql = "INSERT......";
$sth = $db->prepare($sql);

and then try and see the $sql content (using var_dump($sql) for instance). 然后尝试查看$ sql的内容(例如,使用var_dump($sql) )。 This may set you on the right path. 这可能会使您走上正确的道路。

Also, add this after $sth->execute() : 另外,在$sth->execute()之后添加它:

$error = $db->errorInfo();
echo $error[2];

This will show you the MySQL error message, if there is one. 如果有错误消息,它将显示MySQL错误消息。 (Don't forget to remove all this debug output later though.) (不过不要忘记稍后删除所有这些调试输出。)

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

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