0

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!

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Abrar. I amended it slightly: $valueoutput = rtrim($values,','); $query = "INSERT INTO $table_name (shipid, product_sku, product_quantity) VALUES $valueoutput"; $wpdb->query( $wpdb->prepare("$query ", $valueoutput)); rtrim to remove the final comma from the $values variable, then for some reason the $valueoutput wasn't amending the prepare statement.
0

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

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.