0

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.

  • 4
    Obligatory [PDO blurb](http://stackoverflow.com/questions/866860/mysql-versus-pdo)? What does the POST look like? Is it in fact associative? – ficuscr Feb 05 '13 at 17:15
  • 2
    On addslashes, and not using that: http://stackoverflow.com/questions/1286419/escaping-using-slashes-and-then-using-stripslashes-php Also, you probably want to specify your column names explicitly instead of relying on their order & existence implicitly. `insert into lots (column, another, another, terms, etc) values ('', ' $brand'...` – Kzqai Feb 05 '13 at 17:19
  • It is unlikely *foreach* or the *POST* system fail. So it must be at your HTML, user input or you have an old row was still present in DB ... – Déjà vu Feb 05 '13 at 17:24
  • I did a print_r($_post) before the foreach loop and it is sending an empty array for the seconding table, so now I need to try and remove the empty array before running the query. – jmartinez916 Feb 06 '13 at 15:06

2 Answers2

0

Change this

$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);

Into this:

if (trim($_POST['terms'][$key])!='' {
$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);
 }

It should prevent from empty row.

koshin
  • 161
  • 1
  • 5
  • is the "if" statement going inside the foreach loop, I'm getting a syntax error on the opening curly brace. – jmartinez916 Feb 05 '13 at 18:00
  • there was a missing ) before the opening { on the if statement, added the ) and it still inserts a second row with the report_id column with data in it passed from a hidden input on the form. – jmartinez916 Feb 05 '13 at 20:13
0

Thank you everyone for your input. I was able to resolve the issue of the blank row, I checked my html like ring0 suggested and did a print_r($_post) to see what was being posted and the array had empty keys.

Array ( [brand] => Array ( [0] => test [1] => ) [terms] => Array ( [0] => test [1] => ) [hwstamp] => Array ( [0] => yes [1] => ) [origin] => Array ( [0] => mex [1] => ) [cases] => Array ( [0] => 1234 [1] => ) )

so in my foreach loop I did the following to make sure if the user did not use all the form fields not to create a second row with empty data.

foreach ($_POST['brand'] as $key => $brand) 



{

if($brand !=''){


$terms = addslashes($_POST['terms'][$key]);

$hwstamp = addslashes($_POST['hwstamp'][$key]);
$origin = addslashes($_POST['origin'][$key]);
$cases = addslashes($_POST['cases'][$key]);



$queryreg= "INSERT INTO lots VALUES('',' $brand', '$terms',' $hwstamp', '$origin', '$cases','$report_id')" or die(mysql_error());

mysql_query($queryreg);
}

echo mysql_error();
}   

now that it is working I can go in there and clean up the code to prevent sql injections. Thank you to everyone for their input