gpt4 book ai didi

PHP 从多维数组准备插入语句

转载 作者:行者123 更新时间:2023-12-01 00:49:38 24 4
gpt4 key购买 nike

更新后的代码:

if (isset($_POST['submit'])) {

/* Create a prepared statement */

$query = "INSERT INTO log_dates(week_date, crew_chief, monday_crew) values(?,?,?)";

$stmt = mysqli_prepare($connection, $query);


$returnedData = $_POST['data'];

foreach($returnedData as $data) {
$week_date = $data['week_date'];
$crew_chief = $data['crew_chief'];
$monday_crew = $data['monday_crew'];
$stmt->execute();
}


mysqli_stmt_bind_param($stmt, 'sss', $week_date, $crew_chief, $monday_crew);

/* Execute it */
mysqli_stmt_execute($stmt);


/* Close statement */
mysqli_stmt_close($stmt);

} // end if

这是我的 POST 数组的样子:

Array ( [0] => Array ( [week_date] => 2013-07-08 - 2013-07-14 ) [1] => Array ( [crew_chief] => Alan ) [2] => Array ( [monday_crew] => dd ) )

最佳答案

这不是您绑定(bind)参数的方式。您正在将多个参数打成一个单一的值。绑定(bind)调用应该是

mysqli_stmt_bind_param($stmt, 'ss', $foo, $bar);
^^--two params
^^^^^^^^^^---two values


foreach($returnedData as $data) {
$bar = $data['crew_chief'];
$foo = $data['week_date'];
$stmt->execute();
}

一旦变量被绑定(bind),简单地为它们分配新值将导致对语句的下一个 ->execute() 调用获取这些新值。

关于PHP 从多维数组准备插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17535999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com