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.