【问题标题】:Php insert array value into mysql databasephp将数组值插入mysql数据库
【发布时间】:2016-05-26 15:39:14
【问题描述】:

请指教我哪里做错了?

$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);

我没有做对的事情

【问题讨论】:

  • 可能你需要为每个值加上引号,假设它们不是 int
  • 你确定 $data_array 不为空吗?
  • 当前代码会发生什么?错误?

标签: php mysql arrays insert


【解决方案1】:

考虑将查询字符串移动到单独的变量中:

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

然后尝试查看 $sql 内容(例如使用var_dump($sql))。这可能会让你走上正确的道路。

另外,在$sth->execute()之后添加这个:

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

这将向您显示 MySQL 错误消息(如果有)。 (不要忘记稍后删除所有这些调试输出。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-21
    • 2014-12-02
    • 2013-11-30
    • 2011-12-06
    • 2012-04-20
    相关资源
    最近更新 更多