Mark Gerryl Mirandilla
Mark Gerryl Mirandilla

Reputation: 823

insert query inside for loop

I'm currently working on a form that generate fields.

My problem is when I tried to insert to the database, it was successful, however the only data that is being inserted is the last fields.

I tried to just echo the result and it is working fine.

I even try to use foreach but it has the same result.

I hope you can help me.

CODE

require_once 'inc.connection.php';

$shipping_code = generateRandomString();
$packaging = $_POST['packaging']; 
$quantity = $_POST['quantity'];
$total_weight = $_POST['total_weight'];
$length = $_POST['length'];
$width = $_POST['width'];
$height = $_POST['height'];
$unit = $_POST['unit'];
$freight_class = $_POST['freight_class'];



$counter_count = count($packaging);

for($a=0; $a < $counter_count; $a++)
{


    $sql = "INSERT INTO shipping_items (user_id, shipping_code, packaging, quantity, total_weight, length, width, height, unit, freight_class)
    VALUES ('$_SESSION[id]', '$shipping_code', '$packaging[$a]', '$quantity[$a]', '$total_weight[$a]', '$length[$a]', '$width[$a]', '$height[$a]', '$unit[$a]', '$freight_class[$a]')";


}

Upvotes: 0

Views: 555

Answers (1)

Ashu
Ashu

Reputation: 1320

Add the statement : mysql_query($sql) inside the loop... it will solve your problem

Upvotes: 1

Related Questions