gpt4 book ai didi

php - 从 HTML 表单 (WPDB) 更新 MySql 多行

转载 作者:行者123 更新时间:2023-11-29 10:18:31 25 4
gpt4 key购买 nike

我正在尝试创建一个表单来更新/插入 WordPress 站点上的多个 mysql 行。

我认为我的表单结构没问题,但我认为我在创建查询时遇到问题。如果我打印当前的查询,我会得到以下信息:

插入 wp_ck_shipment_products(shipid、product_sku、product_quantity)值数组

超出我的预期:

插入 wp_ck_shipment_products (shipid、product_sku、product_quantity) 值('S123', 'Pro1', '2'), ('S123', 'Pro2', '4')

我的代码如下:

$shipid = '123';
$product_sku = $_POST['product_sku'];
$product_quantity = $_POST['product_quantity'];

if (isset($_POST['insert'])) {
$values = array();

for ( $i=0;$i<count($product_sku);$i++) {
$product_sku = $_POST['product_sku'][$i];
$product_quantity = $_POST['product_quantity'][$i];
};

$values[] = array('shipid' => $shipid[$i], 'product_sku' => $product_sku[$i], 'product_quantity' => $product_quantity[$i]);

$string = implode(" ",$values);
$query = "INSERT INTO $table_name (shipid, product_sku, product_quantity) VALUES ";
$wpdb->query( $wpdb->prepare("$query ", $string));
}

****** HTML Form:

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table class="cktable" >
<tr>
<th>ShipID:</th>
<th>Product SKU:</th>
<th>Quantity:</th>
</tr>
<tr>
<td><input type="text" name="shipid[]" /></td>
<td><input type="text" name="product_sku[]" /></td>
<td><input type="text" name="product_quantity[]" /></td>
</tr>
<tr>
<td><input type="text" name="shipid[]" /></td>
<td><input type="text" name="product_sku[]" /></td>
<td><input type="text" name="product_quantity[]" /></td>
</tr>
</table>
<input type='submit' name="insert" value='Insert'>
</form>

如有任何建议,我们将不胜感激!

最佳答案

Try this one:

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

$shipids = $_POST['shipid'];

$product_skus = $_POST['product_sku'];

$product_quantitys = $_POST['product_quantity'];

$values = '';

$count = count($_POST['product_sku']);


for ( $i=0;$i<=($count-1);$i++) {

$product_sku = $product_skus[$i];

$product_quantity = $product_quantitys[$i];

$shipid = $shipids[$i];

$values .= '('."'".$shipid."'".','."'".$product_sku."'".','."'".$product_quantity."'".'),';
}

$query = "INSERT INTO $table_name (shipid, product_sku, product_quantity) VALUES ";
$wpdb->query( $wpdb->prepare("$query ", $values));
}
?>
****** HTML Form:

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table class="cktable" >
<tr>
<th>ShipID:</th>
<th>Product SKU:</th>
<th>Quantity:</th>
</tr>
<tr>
<td><input type="text" name="shipid[]" /></td>
<td><input type="text" name="product_sku[]" /></td>
<td><input type="text" name="product_quantity[]" /></td>
</tr>
<tr>
<td><input type="text" name="shipid[]" /></td>
<td><input type="text" name="product_sku[]" /></td>
<td><input type="text" name="product_quantity[]" /></td>
</tr>
</table>
<input type='submit' name="insert" value='Insert'>
</form>

关于php - 从 HTML 表单 (WPDB) 更新 MySql 多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745757/

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