I was wondering why my foreach loop is creating an extra row in my mysql table and inserting only one column with data. I'm new to php and mysql so any help is very appreciated. below is the code from my form
<form name="insert_test" action="inserttest.php" method="post">
<table>
<tr>
<td>Brand</td><td>terms</td><td>HW Stamp</td><td>Origin</td><td>Cases</td>
</tr>
<tr>
<td><input type="text" name="brand[]" /></td>
<td><input type="text" name="terms[]" /></td>
<td><input type="text" name="hwstamp[]" /></td>
<td><input type="text" name="origin[]" /></td>
<td><input type="text" name="cases[]" /></td>
</tr>
</table>
<table>
<tr>
<td>Brand</td><td>terms</td><td>HW Stamp</td><td>Origin</td><td>Cases</td>
</tr>
<tr>
<td><input type="text" name="brand[]" /></td>
<td><input type="text" name="terms[]" /></td>
<td><input type="text" name="hwstamp[]" /></td>
<td><input type="text" name="origin[]" /></td>
<td><input type="text" name="cases[]" /></td>
</tr>
</table>
<input type="hidden" name="report_id" value="<?php echo $report_id;?>" />
<input type="submit" value="Submit" />
and this is my php code:
foreach ($_POST['brand'] as $key => $brand) {
$terms = addslashes($_POST['terms'][$key]);
$hwstamp = addslashes($_POST['hwstamp'][$key]);
$origin = addslashes($_POST['origin'][$key]);
$cases = addslashes($_POST['cases'][$key]);
$report_id = addslashes($_POST['report_id'][$key]);
$queryreg= "INSERT INTO lots VALUES('',' $brand', '$terms',' $hwstamp', '$origin', '$cases','$report_id')" or die(mysql_error());
mysql_query($queryreg);
}
echo mysql_error();
the columns in my table are (id,brand,terms,hwstamp,origin,cases,report_id) id is the primary key and report_id is a foreign key. when I enter data into both tables it inserts correctly with two rows created and all the data in the columns, when I only enter data into one table, it creates one row in the table with all the data, but then creates a second row with all columns empty except for the report_id column.