Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting HTML Form $_POST Array Into MySQL Using Implode
    text
    copied!<p>I have the following html submit form. This form is a PHP_SELF and stores the input data as an array in $_POST.</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form method="post" action="&lt;?php echo htmlentities($PHP_SELF); ?&gt;"&gt; &lt;fieldset&gt; &lt;legend&gt;Direct Bill Information:&lt;/legend&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/br&gt; DB #: &lt;input type="int" name="db_num[]" /&gt; DB Amount: &lt;input type="int" name="db_amnt[]" /&gt; &lt;/fieldset&gt; &lt;input type="submit" value="submit" name="submit" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I have a MySQL table that has the fields 'db_num' and 'db_amnt', and I want to insert all the input data in the array into the table. I've looked around and 'implode' seems to be the best way, but after much research I still can't get it to work. The entire PHP executes and finishes without error, however, instead of displaying all 10 rows of both columns, it just displays 2 rows of the two columns with values = 0 for all entries. This is the code I'm using:</p> <pre><code>$sql2 = array(); foreach($_POST as key=&gt;$value) { if(key != 'submit') { if($value != '') { $sql2[] = '("'.mysql_real_escape_string($value['db_num']).'", "'.$value['db_amnt'].'")'; } } } mysql_query('INSERT INTO ' .$tablename_db. '(db_num, db_amnt) VALUES '.implode(',', $sql2)); </code></pre> <p>I did a little debugging and echoed the $value in the foreach loop, and it turns out that it's only displaying three terms; 'Array', 'Array', and 'submit'. Further checks showed that the only way I can get the values from the submit form to show is if I do a foreach loop through $_POST[db_num] and $_POST[db_amnt] instead of just $_POST. I'm not really sure why it's doing this and how to fix it. This is my first code in PHP and I'm learning as I go. I really appreciate any help that I can get!</p>
 

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