0

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();
}
7
  • Are you asking how to bind and execute an array as opposed to a string? Commented Jul 4, 2018 at 15:15
  • How are those arrays indexed - numbered from 0 to n? Commented Jul 4, 2018 at 15:18
  • @qirel yes, that the question. Commented Jul 4, 2018 at 15:49
  • @qirel i have binded the first prepared execute statement Commented Jul 4, 2018 at 15:50
  • @qirel, yes the array is from 0 upward when i do print_r(item_name) Commented Jul 4, 2018 at 15:51

1 Answer 1

2

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

Comments

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.