Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert Multiple rows using implode
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;form action="multiscript.php" method="post" id="form"&gt; &lt;table width="100%" border="0" cellspacing="0" cellpadding="0"&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" name="field_PK[]" id="field_PK" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="sql_fk[]" id="sql_fk" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="account_fk[]" id="account_fk" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="field_code[]" id="field_code" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="field_name[]" id="field_name" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="field_px[]" id="field_px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="field_order[]" id="field_order" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" name="field_PK[]" id="field_PK" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="sql_fk[]" id="sql_fk" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="account_fk[]" id="account_fk" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="field_code[]" id="field_code" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="field_name[]" id="field_name" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="field_px[]" id="field_px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="field_order[]" id="field_order" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type="submit" name="button" id="button" value="Submit" /&gt; &lt;/form&gt; </code></pre> <p>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:</p> <pre><code>&lt;?php $values = array();?&gt; &lt;?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); } ?&gt; </code></pre> <p>I know there is something wrong because when i "print" the query i get this: </p> <pre><code>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) </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload