简体   繁体   中英

How to insert multiple records in different rows in prepared statement php

I have inserted some strings with values in one table lets call it table_1 in my database, now I have arrays which I want to insert into separate rows in table_2 in my SQL database.

$sql = 'INSERT INTO ' . $table_1 . '(shipping_fee, waybill_status, pickup_fee, ) 
        VALUES(:shipping_fee, :waybill_status, :pickup_fee)';

$stmt = $this->dbConn->prepare($sql);
$stmt->bindParam(':shipping_fee', $s_shipping_fee);
$stmt->bindParam(':waybill_status', $s_waybill_status);
$stmt->bindParam(':pickup_fee', $this->pickup_fee);

if($stmt->execute()){ //THIS INSERTED THE STRINGS PERFECTLY
    //NOW ALL VALUES TO BE INSERT INTO $sqal is an array

    $sqal = 'INSERT INTO ' . $table_2. '(id, waybill_number, client_id, item_name, item_weight, item_length, item_width, item_category, date_added) VALUES(null, :waybill_numberr, :client_idaa, :item_name, :item_weight, :item_length, :item_width, :item_category, :date_added)';

    $stmtaaa = $this->dbConn->prepare($sqal);
    $stmtaaa->bindParam(':item_name', $this->item_name); //ARRAY
    $stmtaaa->bindParam(':item_weight', $this->item_weight); //ARRAY
    $stmtaaa->bindParam(':item_length', $this->item_length); //ARRAY
    $stmtaaa->bindParam(':item_width', $this->item_width); //ARRAY
    $stmtaaa->bindParam(':item_category', $this->item_category); //ARRAY

    $stmtaaa->execute(); //HoW do I go about this.
} else {
    echo "Could not insert";
    exit();
}

You had a syntax error in your first query, the trailing commas , should not be there in the column- or value-list.

You can insert an array by executing the prepare multiple times with different values. This example assumes that all your arrays are indexed by numbers (from zero and up).

The code example above also binds more columns than it binds, so you need to bind a value to each column. waybill_numberr , client_idaa and date_added are missing its binds (I just added some random placeholders).

$sql = "INSERT INTO $table_1 (shipping_fee, waybill_status, pickup_fee) 
        VALUES (:shipping_fee, :waybill_status, :pickup_fee)";

$stmt = $this->dbConn->prepare($sql);
$stmt->bindParam(':shipping_fee', $s_shipping_fee);
$stmt->bindParam(':waybill_status', $s_waybill_status);
$stmt->bindParam(':pickup_fee', $this->pickup_fee);

if ($stmt->execute()) {
    $sqal = "INSERT INTO $table_2 (id, waybill_number, client_id, item_name, item_weight, item_length, item_width, item_category, date_added) 
             VALUES (null, :waybill_numberr, :client_idaa, :item_name, :item_weight, :item_length, :item_width, :item_category, :date_added)";

    $stmtaaa = $this->dbConn->prepare($sqal);

    foreach ($this->item_weight as $key => $value) {
        $stmtaaa->execute(["waybill_numberr" => '1',   // Change this to your actual value
                           "client_idaa" => '1',       // Change this to your actual value
                           "item_name" => $value, 
                           "item_weight" => $this->item_weight[$key],
                           "item_length" => $this->item_length[$key],
                           "item_width" => $this->item_width[$key],
                           "item_category" => $this->item_category[$key],
                           "date_added" => '1']);
    }
} else {
    echo "Could not insert";
    exit();
}

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