Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I insert a simple array into a database (php/MySQL)?
    primarykey
    data
    text
    <p>I'm working on a form in which the user makes a few selections, and then each selected item's name needs to go into a table in a db. The user's name can go with each item as an identifier for the user's selections. The number of selections, and the names of the selections will change but the two fields in the database remain constant. My problem is that on insert, I get an error that there is no column with the name specified in the VALUE I'm sending. Its tripped me all up....can't figure out why. Thanks a lot for any help or advice. </p> <p>Full code:</p> <pre><code>&lt;?php $db = mysql_connect('localhost', 'root', 'root') or die(mysql_error()); mysql_select_db('reflex') or die(mysql_error()); $name_array = array('harp1','harp2','harp3'); if (isset($_POST['interests'])) { $interests_str = implode(" ", $_POST['interests']);// converts $_POST interests into a string $interests_array = explode(" ", $interests_str);// converts the string to an array which you can easily manipulate foreach ($name_array as $row=&gt;$name) { $username = mysql_real_escape_string($name); $photo = mysql_real_escape_string($_POST['interests'][$row]); mysql_query('INSERT INTO photos (username, photo) VALUES ("' . $username . '", ' . $photo . ')') or die(mysql_error()); } } ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;array test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="array.php" method="post"&gt; &lt;input type="checkbox" name="interests[]" value="Politics" /&gt;Politics&lt;br /&gt; &lt;input type="checkbox" name="interests[]" value="Entertainment" /&gt;&lt;br /&gt; &lt;input type="checkbox" name="interests[]" value="Tech" /&gt;&lt;br /&gt; &lt;input type="checkbox" name="interests[]" value="Health" /&gt;&lt;br /&gt; &lt;input type="checkbox" name="interests[]" value="Living" /&gt;&lt;br /&gt; &lt;input type="checkbox" name="interests[]" value="Travel" /&gt;&lt;br /&gt; &lt;input type="checkbox" name="interests[]" value="World" /&gt;&lt;br /&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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