我想使用bindParam.
这是我的代码的较短版本:
$reindex['f_name'] = 'Tom';
$reindex['l_name'] = 'Riddle';
$reindex['date'] = '2020-12-12';
$sql = "INSERT INTO tbl_user (f_name, l_name, date) VALUES (:f_name, :l_name, :date)";
$stmt = $pdo->prepare($sql);
foreach ($reindex as $key => $value) {
echo $key . '<br>'; // look at output
echo $value . '<br><br>';
$stmt->bindParam($key, $value);
}
$stmt->execute();
回声输出:
f_name
Tom
l_name
Riddle
我没有收到任何错误消息。我这样设置错误报告:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
这可行,但我必须单独定义每个绑定:
$stmt->execute([
'f_name' => $reindex['f_name'],
'l_name' => $reindex['l_name'],
'date' => $reindex['date']
]);
守候你守候我