gpt4 book ai didi

php - 使用 for 循环将值添加到 MySQL 插入查询

转载 作者:行者123 更新时间:2023-11-29 23:17:30 24 4
gpt4 key购买 nike

PHP:

$bookquery = "SELECT * FROM heli_book ORDER BY book_id DESC LIMIT 1";
$bookget = mysql_query("$bookquery");
$booking = mysql_fetch_assoc($bookget, MYSQL_ASSOC);

$custquery = "SELECT cust_id FROM heli_cust LIMIT ".$total."";
$custget = mysql_query("$custquery");
$cust = mysql_fetch_assoc($custget, MYSQL_ASSOC);

$bcusquery = "INSERT INTO heli_bcus (bcus_book, bcus_cust) VALUES ";

for($x = 0; $x < count($cust_fname); $x++) {
$bcusquery .= "('".$bookid."', '".$custid."')";
if ($x+1 != $total) {
$bcusquery .= ", ";
}
}

echo "<br>";
echo $bcusquery;

我试图让 $custid 对数组进行排序,但它始终作为数组中的第一项出现。

当前结果:('01', 'A'), ('01', 'A'), ('01', 'A')

期望结果:
('01', 'A'), ('01', 'B'), ('01', 'C')

$totalcount(array_filter($cust_fname))
的值$cust_fname = array_filter($_POST[cust_fname]);

最佳答案

成功了!

为此 - 将 $booking = mysql_fetch_assoc($bookget, MYSQL_ASSOC); 移至 for 循环中!

即:

        $bookquery = "SELECT * FROM heli_book ORDER BY book_id DESC LIMIT 1";
$bookget = mysql_query("$bookquery");
$booking = mysql_fetch_assoc($bookget, MYSQL_ASSOC);

$custquery = "SELECT cust_id FROM heli_cust LIMIT ".$total."";
$custget = mysql_query("$custquery");


$bcusquery = "INSERT INTO heli_bcus (bcus_book, bcus_cust) VALUES ";

$custid = $cust[cust_id];
$bookid = $booking[book_id];

for($x = 0; $x < count($cust_fname); $x++) {
$cust = mysql_fetch_assoc($custget, MYSQL_ASSOC);
$bcusquery .= "('".$bookid."', '".$cust[cust_id]."')";
if ($x+1 != $total) {
$bcusquery .= ", ";
}
}

echo "<br>";
echo $bcusquery;

关于php - 使用 for 循环将值添加到 MySQL 插入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27635003/

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