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

时间:2018-04-10 05:12:27

标签: php mysql wordpress

我试图在wordpress网站上创建一个表单来更新/插入多个mysql行。

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

INSERT INTO wp_ck_shipment_products(shipid,product_sku,product_quantity)VALUES 数组

而不是我的期望:

INSERT INTO wp_ck_shipment_products(shipid,product_sku,product_quantity)VALUES (' 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>

任何建议都会非常感激!

2 个答案:

答案 0 :(得分:1)

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>

答案 1 :(得分:0)

您可以像这样更改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));
}