Virik Virik - 3 months ago 8
MySQL Question

Each form item as new row in mySql table

I can't figure this out. Any help is appreciated.

We have a form where the user may add unlimited items.. Like this:

product[]
amount[]
qty[]
total[]

product[]
amount[]
qty[]
total[]

product[]
amount[]
qty[]
total[]


When the form is submitted, we need to create one table row for each item in our mysql database.

ID Qty Amount Total
1 2 100 200
2 1 20 20
3 2 50 100


We use ajax to submit the form, and PHP to create the order in our database... but how to make a new table row for each item?

I'm lost. Need your help :)

Answer
$cnt=count($product);
$product=array_map('intval',$product);//$_REQUEST['product'];
$amount=array_map('intval',$amount);
$qty=array_map('intval',$qty);
$total=array_map('intval',$total);
for($i=0;$i<$cnt;$i++){
    $rows[]="({$product[$i]},{$qty[$i]},{$amount[$i]},{$total[$i]})";
}
print "INSERT INTO tablename (ID ,Qty ,Amount ,Total) VALUES ".implode(', ',$rows);

The rest is one you...