从html表单(wpdb)更新mysql多行

8mmmxcuj  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(280)

我正在尝试创建一个表单来更新/插入wordpress站点上的多个mysql行。
我想我的表单结构还可以,但是我想我在创建查询时遇到了问题。如果打印当前查询,将得到以下结果:
插入wp\u ck\u shipment\u products(shipid、product\u sku、product\u quantity)值数组
而不是我的预期:
将值('s123','pro1','2'),('s123','pro2','4')插入wp\u ck\u shipping\u products(shipid,product\u sku,product\u quantity)
我的代码如下:

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

任何建议都将不胜感激!

vsikbqxv

vsikbqxv1#

您可以这样更改php脚本:

global $wpdb;
$shipid = $_POST['shipid'];
$product_sku = $_POST['product_sku'];
$product_quantity = $_POST['product_quantity'];

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

    for ( $i=0;$i<count($product_sku);$i++) {
        $values[] = "('".$shipid[$i]."', '".$product_sku[$i]."', '".$product_quantity[$i]."')";
    };

    $string = implode(",",$values);
    $query = "INSERT INTO %s (shipid, product_sku, product_quantity) VALUES %s";
    $wpdb->query($wpdb->prepare("$query", 'your_table', $string));
}
sqxo8psd

sqxo8psd2#

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>

相关问题