1

I am having a hard time getting my code to work. I grasp the basic concept but can't get it to work. I have a form with multiple rows:

<form action="multiscript.php" method="post" id="form">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="text" name="field_PK[]" id="field_PK" /></td>
<td><input type="text" name="sql_fk[]" id="sql_fk" /></td>
<td><input type="text" name="account_fk[]" id="account_fk" /></td>
<td><input type="text" name="field_code[]" id="field_code" /></td>
<td><input type="text" name="field_name[]" id="field_name" /></td>
<td><input type="text" name="field_px[]" id="field_px" /></td>
<td><input type="text" name="field_order[]" id="field_order" /></td>
</tr>
<tr>
<td><input type="text" name="field_PK[]" id="field_PK" /></td>
<td><input type="text" name="sql_fk[]" id="sql_fk" /></td>
<td><input type="text" name="account_fk[]" id="account_fk" /></td>
<td><input type="text" name="field_code[]" id="field_code" /></td>
<td><input type="text" name="field_name[]" id="field_name" /></td>
<td><input type="text" name="field_px[]" id="field_px" /></td>
<td><input type="text" name="field_order[]" id="field_order" /></td>
</tr>
</table>
<input type="submit" name="button" id="button" value="Submit" />
</form>

I then have a script to process the form that I wish to insert multiple rows into my mysql database. My script looks like this:

<?php 
$values = array();?>
<?php
foreach( $_POST as $row ) { 
$values[] =  "(" . $row['field_PK'] . "," . $row['sql_fk'] . "," . $row['account_fk']. "," . $row ['field_code']. "," . $row['field_name']. "," . $row['field_px']. "," . $row['field_order'].")";
}
if( !empty($values) ) {
$query = "INSERT INTO `Table_fields` (field_PK, sql_fk, account_fk, field_code, field_name, field_px, field_order) VALUES ". implode(',',$values);

mysql_query($query);
}
?>

I know there is something wrong because when i "print" the query i get this:

INSERT INTO `Table_fields` (field_PK, sql_fk, account_fk, field_code, field_name, field_px, field_order) VALUES (,,,,,,),(,,,,,,),(,,,,,,),(,,,,,,),(,,,,,,),(,,,,,,),(,,,,,,),(S,S,S,S,S,S,S) 

Can someone please look over my form and script and tell me what I am doing wrong and why it is not inserting the mutliple rows into my database using the implode method. Thank you.

user982853
  • 2,470
  • 14
  • 55
  • 82

3 Answers3

1

Your $_POST superglobal actually looks like this:

Array
(
    [field_PK] => Array
        (
            [0] => value1
            [1] => value2
        )
    [sql_fk] => ...
    [account_fk] => ...
    [field_code] => ...
    [field_name] => ...
    [field_px] => ...
    [field_order] => ...
    [button] => Submit
)

When you do foreach( $_POST as $row ) you are looping through each field instead of each row of fields as you expected. As such, you get mostly blanks (I'm guessing you submitted a blank form to test it) and then the 'S' from Submit.

You can do it the way you want to, but you need to transpose your $_POST values first. This answer has a very simple transpose function. (He calls it "flipDiagonally", but it's a typical transpostion).

Using the function from the linked answer, you could do like this:

$transposed = transpose( $_POST );

foreach( $transposed as $index => $row ) { 
    $values[] = "(" . $row['field_PK'] . "," . $row['sql_fk'] . "," . $row['account_fk']. "," . $row ['field_code']. "," . $row['field_name']. "," . $row['field_px']. "," . $row['field_order'].")";
}
Community
  • 1
  • 1
Farray
  • 8,290
  • 3
  • 33
  • 37
  • I cut an pasted the function code and mad the change in my code and now it's telling me - Warning: array_map() [function.array-map]: Argument #9 should be an array in /nfs/c06/h04/mnt/95658/domains/bc.wf-realty.com/html/contacts/multiscript.php on line 5 Warning: Invalid argument supplied for foreach() in /nfs/c06/h04/mnt/95658/domains/bc.wf-realty.com/html/contacts/multiscript.php on line 11 – user982853 Oct 06 '11 at 20:33
  • @user982853 Alter your `foreach` to use key-value pairings. Updated the answer to reflect this. – Farray Oct 06 '11 at 20:51
0

Try

foreach($_POST as $index => $row)

opposed to

foreach($_POST as $values)

Of course, don't forget the usual story about SQL injection attacks and similar.

Edit:

seeing that all the fields are text type, regardless of whether they're empty or not - they'll be sent. They also share the same indexes, so you can type it in a more elegant manner:

foreach($_POST['field_pk'] as $index => $values)
{
    $values[] =  "(" . $_POST[$index]['field_PK'] . "," . $_POST[$index]['sql_fk'] . "," . $_POST[$index]['account_fk']. "," . $_POST[$index]['field_code']. "," . $_POST[$index]['field_name']. "," . $_POST[$index]['field_px']. "," . $_POST[$index]['field_order'].")";

}

I must admit, not reading properly led to me giving out the wrong answer. Same SQL-injection prevention rule applies, make sure your input is filtered!

Furicane
  • 1,173
  • 6
  • 18
  • Let me start by saying thank you for responding. I tried your suggestion but it is still not working. My code now looks like this: $row ) { $values[] = "(" . $row['field_PK'] . "," . $row['sql_fk'] . "," . $row['account_fk']. "," . $row['field_code']. "," . $row['field_name']. "," . $row['field_px']. "," . $row['field_order'].")"; } if( !empty($values) ) { print $query = "INSERT INTO `Table_fields` (field_PK, sql_fk, account_fk, field_code, field_name, field_px, field_order) VALUES ". implode(',',$values); mysql_query($query); } ?> – user982853 Oct 06 '11 at 19:41
  • I tried the above but im not sure if i am doing it correctly? When i print the query i still get: INSERT INTO `Table_fields` (field_PK, sql_fk, account_fk, field_code, field_name, field_px, field_order) VALUES (,,,,,,),(,,,,,,) . I am not submitting a blank form, when i print_r the $_Post array it has data. – user982853 Oct 06 '11 at 20:10
  • 1
    This answer is incorrect. `$_POST` is structured using multiple indexed array values for each field name. `$_POST[$index][$fieldName]` won't return anything. You need to use `$_POST[$fieldName][$index]` but this would still be poor form based on your `foreach` loop. – Farray Oct 06 '11 at 20:23
  • So then can someone tell me what the "correct" form is to achieve my insert multiple row from post form using implode. My code is above and it would probably be the most helpful if you repost my entire code the way it is supposed to look, the correct code. Thanks again. – user982853 Oct 06 '11 at 20:53
  • @user982853 I've given you a working solution in my answer and explained the problem in your logic. This is how SO is intended to work. Teaching a man to fish vs giving a man a fish, and other clever parables like that... – Farray Oct 06 '11 at 21:06
  • @Farray thank you for you help. I did get it to work with the function but i am getting an error in my code. "Warning: Invalid argument supplied for foreach() in /nfs/c06/h04/mnt/95658/domains/bc.wf-realty.com/html/contacts/multiscript.php on line 6" this error on line 6 is part of the function i am referencing. Any ideas on why the im getting the error? Line six looks like this: foreach ($subarr as $subkey => $subvalue) {. Thanks again. – user982853 Oct 06 '11 at 22:06
  • @user982853 There is no way to help you figure out what is going on using just a snippet of code from the offending line. You should use [`print_r`](http://php.net/manual/en/function.print-r.php) or [`var_dump`](http://www.php.net/manual/en/function.var-dump.php) to inspect your variables and determine where the problem is. I can copy/paste your code, copy/paste my answer, rename 1 function, and have a working script. I'm not sure what other changes you have made that are causing errors. – Farray Oct 06 '11 at 22:11
  • Even though its throwing this error the records are still getting added to the db so im not sure if i should just ignor it. Thanks again. – user982853 Oct 06 '11 at 22:30
0

You don't want to iterate over all POST variables. Instead you want to iterate over the $_POST["field_PK"] arrays.

Tobias
  • 9,170
  • 3
  • 24
  • 30
  • Thank you for your response. I added field_PK but am still getting the same error. My script now looks like this: $row ) { $values[] = "(" . $row['field_PK'] . "," . $row['sql_fk'] . "," . $row['account_fk']. "," . $row['field_code']. "," . $row['field_name']. "," . $row['field_px']. "," . $row['field_order'].")"; } if( !empty($values) ) { print $query = "INSERT INTO `Table_fields` (field_PK, sql_fk, account_fk, field_code, field_name, field_px, field_order) VALUES ". implode(',',$values); mysql_query($query); } ?> – user982853 Oct 06 '11 at 19:47