简体   繁体   中英

Update MySql multiple rows from HTML form (WPDB)

I'm trying to create a form to update/insert multiple mysql rows on a wordpress site.

I think my form structure is ok but I think I'm having trouble creating the query. If I print my current query, I get the following:

INSERT INTO wp_ck_shipment_products (shipid, product_sku, product_quantity) VALUES Array

Rather than my expected:

INSERT INTO wp_ck_shipment_products (shipid, product_sku, product_quantity) VALUES ('S123', 'Pro1', '2'), ('S123', 'Pro2', '4')

My code is as follows:

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

Any suggestions would be really appreciated!

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>

You can change php script like this:

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM