title: Trying to insert option value data in to the MySQL database using PHP from Dropdown Website Form created in twitter-bootstrap
body: <p>I am a beginner with php.  I am using twitter-bootstrap to create a website.  I would like to store the data submitted from a website dropdown menu into a MySQL database named datawebcollectiondb . I am able to store information in the db when the user submits input from an open text field, but not from a drop down menu.  The database fills with 0s in all fields when the dropdown menu is submitted.</p>
  
  <p>First the open text field that did work:</p>
  
  <p>HTML file:</p>
  
  <pre><code>&lt;html&gt;
              &lt;body&gt;
                  &lt;form action="insert.php" method="post"&gt;
                      Number Please: &lt;input type="text" name="directionp"&gt;
                          Another Number: &lt;input type="text" name="timep"&gt;
                              Third Number: &lt;input type="text" name="locationp"&gt;
                                Fourth Number: &lt;input type="text" name="searchtimep"&gt;
                                  &lt;input type="submit"&gt;
                  &lt;/form&gt;
  
              &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>PHP file that worked:</p>
  
  <pre><code>&lt;?php
      $con = mysql_connect("host", "username", "password");
      if (!$con)
      {
          die('Could not connect: ' . mysql_error());
      }
  
      mysql_select_db("timeforparkingdb", $con);
  
      $sql="INSERT INTO datawebcollection (directionp, timeofdayp, locationp, searchtimep)
      VALUES
      ('$_POST[directionp]','$_POST[timep]','$_POST[locationp]','$_POST[searchtimep]')";
  
      if (!mysql_query($sql,$con))
      {
          die('Error: ' . mysql_error());
      }
      echo "1 more record added =) ";
  
      mysql_close($con);
  ?&gt;
  </code></pre>
  
  <p>HTML file that did NOT work in tandem with above php file, other than populating db with 0s:</p>
  
  <pre><code>&lt;html&gt;
      &lt;form action="insert.php" method="post"&gt;
                          &lt;b&gt;DIRECTION:&lt;/b&gt;
                              &lt;select name="directionp"&gt;
                                  &lt;option value="1"&gt;Coming&lt;/option&gt;
                                  &lt;option value="2"&gt;Going&lt;/option&gt;
                              &lt;/select&gt;
                          &lt;b&gt;TIME:&lt;/b&gt;
                              &lt;select name="timep"&gt;
                                  &lt;option value="0"&gt;12:00am - 1:00am&lt;/option&gt;
                                  &lt;option value="1"&gt;1:00am - 2:00am&lt;/option&gt;
                                  &lt;option value="2"&gt;2:00am - 3:00am&lt;/option&gt;
                                  &lt;option value="3"&gt;3:00am - 4:00am&lt;/option&gt;
                                  &lt;option value="4"&gt;4:00am - 5:00am&lt;/option&gt;
                                  &lt;option value="5"&gt;5:00am - 6:00am&lt;/option&gt;
                                  &lt;option value="6"&gt;6:00am - 7:00am&lt;/option&gt;
                                  &lt;option value="7"&gt;7:00am - 8:00am&lt;/option&gt;
                                  &lt;option value="8"&gt;8:00am - 9:00am&lt;/option&gt;
                                  &lt;option value="9"&gt;9:00am - 10:00am&lt;/option&gt;
                                  &lt;option value="10"&gt;10:00am - 11:00am&lt;/option&gt;
                                  &lt;option value="11"&gt;11:00am - 12:00pm&lt;/option&gt;
                                  &lt;option value="12"&gt;12:00pm - 1:00pm&lt;/option&gt;
                              &lt;/select&gt;
                          &amp;nbsp;&lt;b&gt;LOCATION:&lt;/b&gt;
                              &lt;select name="locationp"&gt;
                                  &lt;option value="1"&gt;Zone One&lt;/option&gt;
                                  &lt;option value="2"&gt;Zone Two&lt;/option&gt;
                                  &lt;option value="3"&gt;Zone Three&lt;/option&gt;
                                  &lt;option value="4"&gt;Zone Four&lt;/option&gt;
                                  &lt;option value="5"&gt;Zone Five&lt;/option&gt;
                                  &lt;option value="6"&gt;Zone Six&lt;/option&gt;
                                  &lt;option value="7"&gt;Zone Seven&lt;/option&gt;
                                  &lt;option value="8"&gt;Zone Eight&lt;/option&gt;
                                  &lt;option value="9"&gt;Zone Nine&lt;/option&gt;
                                  &lt;option value="10"&gt;Zone Ten&lt;/option&gt;
                                  &lt;option value="11"&gt;Zone Eleven&lt;/option&gt;
                                  &lt;option value="12"&gt;Zone Twelve&lt;/option&gt;
                                  &lt;option value="13"&gt;Zone Thirteen&lt;/option&gt;
                              &lt;/select&gt;
                          &lt;b&gt;SEARCH TIME HERE:&lt;/b&gt;
                          &lt;select name="searchtimep"&gt;
                              &lt;option value="0"&gt;Under 1 Minute&lt;/option&gt;
                              &lt;option value="1"&gt;1 Minute - 3 Minutes&lt;/option&gt;
                              &lt;option value="5"&gt;3 Minutes - 5 Minutes&lt;/option&gt;
                              &lt;option value="10"&gt;5 Minutes - 10 Minutes&lt;/option&gt;
                              &lt;option value="15"&gt;10 Minutes -15 Minutes&lt;/option&gt;
                              &lt;option value="20"&gt;15 Minutes - 20 Minutes&lt;/option&gt;
                              &lt;option value="25"&gt;20 Minutes - 25 Minutes&lt;/option&gt;
                              &lt;option value="30"&gt;25 Minutes - 30 Minutes&lt;/option&gt;
                              &lt;option value="35"&gt;30 Minutes - 35 Minutes&lt;/option&gt;
                              &lt;option value="40"&gt;35 Minutes - 40 Minutes&lt;/option&gt;
                              &lt;option value="45"&gt;40 Minutes - 45 Minutes&lt;/option&gt;
                              &lt;option value="50"&gt;45 Minutes - 50 Minutes&lt;/option&gt;
                              &lt;option value="55"&gt;50 Minutes - 55 Minutes&lt;/option&gt;
                              &lt;option value="60"&gt;55 Minutes - 1 HOUR&lt;/option&gt;
                              &lt;option value="0"&gt;Over 1 HOUR&lt;/option&gt;
                          &lt;/select&gt;
      &lt;button type="submit" class="btn btn-large btn-primary"&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Original MySQL Database Structure:</p>
  
  <pre><code>Field           Type    Collation   Attributes  Null    Default Extra   Action
      unique_entry_id int(11)         No                                  
      dayp            int(2)          No                                  
      monthp          int(2)          No                                  
      yearp           int(4)          No                                  
      directionp  int(1)          No                                  
      timeofdayp  int(2)          No                                  
      locationp   int(2)          No                                  
      searchtimep int(2)          No  
  </code></pre>
  
  <p>What I see inside my database so far:</p>
  
  <pre><code>unique_entry_id dayp    monthp  yearp   directionp  timeofdayp  locationp   searchtimep
              0   6   6   6   0   0   0   0
              0   7   7   7   0   0   0   0
              0   8   24  2345    0   0   0   0
              0   0   0   0   0   0   0   0
              0   0   0   0   0   0   0   0
              0   0   0   0   0   0   0   0
              0   0   0   0   0   0   0   0
              0   0   0   0   0   0   0   0                               
  </code></pre>
  
  <p>The rows with values other than 0 are from when I ran the original code, and the rows with only 0s are from the dropdown form. How can I get the correct correlated option value to be the value stored into the database? I would have thought that the value assigned to the option, i.e. <code>&lt;option value="1"&gt;Zone One&lt;/option&gt; &lt;option value="2"&gt;Zone Two&lt;/option&gt;</code>, would have been the value submitted to the db.</p>
  
  <p>What I saw on my chrome developer console under <code>form data</code>:</p>
  
  <pre><code>direction=2&amp;time=5&amp;location=5&amp;searchtime=25
  </code></pre>
  
  <p>and still the database recorded all 0s.</p>
  
  <p>Corrected MySQL Database Structure:
  Present Corrected Database Structure: </p>
  
  <pre><code>Field           Type          Collation         Attributes  Null    Default Extra   
  unique_entry_id int(11)                 No                                  
  city_id         varchar(50)   utf8_general_ci   No                                   
  datep           date                    No                                  
  directionp  int(1)                  No                                  
  timeofdayp  time                    No                                  
  locationp   int(2)                  No                                  
  searchtimep int(2)                  No  
  </code></pre>
  
  <p>In order to pass integers and not text to the mySQL db from my php code I've removed the single quotes:</p>
  
  <pre><code>&lt;?php
      $con = mysql_connect("", "", "");
      if (!$con)
      {
          die('Could not connect: ' . mysql_error());
      }
  
      mysql_select_db("timeforparkingdb", $con);
  
      $sql="INSERT INTO datawebcollection (directionp, timeofdayp, locationp, searchtimep)
      VALUES
      ($_POST[directionp],$_POST[timep],$_POST[locationp],$_POST[searchtimep])";
  
      if (!mysql_query($sql,$con))
      {
          die('Error: ' . mysql_error());
      }
      echo "Thank you for your submission! 1 more record added =) ";
  
      mysql_close($con);
  ?&gt;
  </code></pre>
  
  <p>First step in order to avoid SQL injection I have modified my HTML to:</p>
  
  <pre><code>&lt;form action="insert.php" id="timeforparking" method="post"&gt;
       &lt;input type="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>DIRECTION:
  &lt;select form = "timeforparking" id="directionp"&gt;
                              &lt;option value="1"&gt;Parking In Manhattan&lt;/option&gt;
                              &lt;option value="2"&gt;Leaving Manhattan&lt;/option&gt;
  &lt;/select&gt;
  TIME:
  &lt;select form = "timeforparking" id="timep"&gt;
                              &lt;option value="0"&gt;12:00am - 1:00am&lt;/option&gt;
                              &lt;option value="1"&gt;1:00am - 2:00am&lt;/option&gt;
                              &lt;option value="2"&gt;2:00am - 3:00am&lt;/option&gt;
                              &lt;option value="3"&gt;3:00am - 4:00am&lt;/option&gt;
                              &lt;option value="4"&gt;4:00am - 5:00am&lt;/option&gt;
                              &lt;option value="5"&gt;5:00am - 6:00am&lt;/option&gt;
                              &lt;option value="6"&gt;6:00am - 7:00am&lt;/option&gt;
                              &lt;option value="7"&gt;7:00am - 8:00am&lt;/option&gt;
                              &lt;option value="8"&gt;8:00am - 9:00am&lt;/option&gt;
                              &lt;option value="9"&gt;9:00am - 10:00am&lt;/option&gt;
                              &lt;option value="10"&gt;10:00am - 11:00am&lt;/option&gt;
                              &lt;option value="11"&gt;11:00am - 12:00pm&lt;/option&gt;
                              &lt;option value="12"&gt;12:00pm - 1:00pm&lt;/option&gt;
                              &lt;option value="13"&gt;1:00pm - 2:00pm&lt;/option&gt;
                              &lt;option value="14"&gt;2:00pm - 3:00pm&lt;/option&gt;
                              &lt;option value="15"&gt;3:00pm - 4:00pm&lt;/option&gt;
                              &lt;option value="16"&gt;4:00pm - 5:00pm&lt;/option&gt;
                              &lt;option value="17"&gt;5:00pm - 6:00pm&lt;/option&gt;
                              &lt;option value="18"&gt;6:00pm - 7:00pm&lt;/option&gt;
                              &lt;option value="19"&gt;7:00pm - 8:00pm&lt;/option&gt;
                              &lt;option value="20"&gt;8:00pm - 9:00pm&lt;/option&gt;
                              &lt;option value="21"&gt;9:00pm - 10:00pm&lt;/option&gt;
                              &lt;option value="22"&gt;10:00pm - 11:00am&lt;/option&gt;
                              &lt;option value="23"&gt;11:00am - 12:00pm&lt;/option&gt;
  &lt;/select&gt;
  LOCATION:
  &lt;select form = "timeforparking" id="locationp"&gt;
                              &lt;option value="1"&gt;Zone One&lt;/option&gt;
                              &lt;option value="2"&gt;Zone Two&lt;/option&gt;
                              &lt;option value="3"&gt;Zone Three&lt;/option&gt;
                              &lt;option value="4"&gt;Zone Four&lt;/option&gt;
                              &lt;option value="5"&gt;Zone Five&lt;/option&gt;
                              &lt;option value="6"&gt;Zone Six&lt;/option&gt;
                              &lt;option value="7"&gt;Zone Seven&lt;/option&gt;
                              &lt;option value="8"&gt;Zone Eight&lt;/option&gt;
                              &lt;option value="9"&gt;Zone Nine&lt;/option&gt;
                              &lt;option value="10"&gt;Zone Ten&lt;/option&gt;
                              &lt;option value="11"&gt;Zone Eleven&lt;/option&gt;
                              &lt;option value="12"&gt;Zone Twelve&lt;/option&gt;
                              &lt;option value="13"&gt;Zone Thirteen&lt;/option&gt;
  &lt;/select&gt;
  
  REPORT YOUR APPROXIMATE SEARCH TIME HERE:
  &lt;select form = "timeforparking" id="searchtimep"&gt;
                          &lt;option value="0"&gt;Under 1 Minute&lt;/option&gt;
                          &lt;option value="1"&gt;1 Minute - 3 Minutes&lt;/option&gt;
                          &lt;option value="5"&gt;3 Minutes - 5 Minutes&lt;/option&gt;
                          &lt;option value="10"&gt;5 Minutes - 10 Minutes&lt;/option&gt;
                          &lt;option value="15"&gt;10 Minutes -15 Minutes&lt;/option&gt;
                          &lt;option value="20"&gt;15 Minutes - 20 Minutes&lt;/option&gt;
                          &lt;option value="25"&gt;20 Minutes - 25 Minutes&lt;/option&gt;
                          &lt;option value="30"&gt;25 Minutes - 30 Minutes&lt;/option&gt;
                          &lt;option value="35"&gt;30 Minutes - 35 Minutes&lt;/option&gt;
                          &lt;option value="40"&gt;35 Minutes - 40 Minutes&lt;/option&gt;
                          &lt;option value="45"&gt;40 Minutes - 45 Minutes&lt;/option&gt;
                          &lt;option value="50"&gt;45 Minutes - 50 Minutes&lt;/option&gt;
                          &lt;option value="55"&gt;50 Minutes - 55 Minutes&lt;/option&gt;
                          &lt;option value="60"&gt;55 Minutes - 1 HOUR&lt;/option&gt;
                          &lt;option value="0"&gt;Over 1 HOUR&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>And I will have to do something to the php to avoid injection, and I do not undertand what I need to do for this yet, and I am presently researching this at <a href="http://php.net/manual/en/pdo.prepared-statements.php" rel="nofollow noreferrer">http://php.net/manual/en/pdo.prepared-statements.php</a>, and trying to apply the following to my case:</p>
  
  <pre><code>This example performs an INSERT query by substituting a name and a value for the named placeholders.
      &lt;?php
      $stmt = $dbh-&gt;prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
      $stmt-&gt;bindParam(':name', $name);
      $stmt-&gt;bindParam(':value', $value);
  
      // insert one row
      $name = 'one';
      $value = 1;
      $stmt-&gt;execute();
  
      // insert another row with different values
      $name = 'two';
      $value = 2;
      $stmt-&gt;execute();
      ?&gt;
  </code></pre>
  
  <p>It may be that some of my values are empty. Next I hope to check the POST parameters and print them out in the php code via <a href="https://stackoverflow.com/questions/196664/print-out-post-values">Print out post values</a> , specifically:</p>
  
  <pre><code>&lt;?php
  // loop through every form field
  while( list( $field, $value ) = each( $_POST )) {
     // display values
     if( is_array( $value )) {
        // if checkbox (or other multiple value fields)
        while( list( $arrayField, $arrayValue ) = each( $value )) {
           echo "&lt;p&gt;" . $arrayValue . "&lt;/p&gt;\n";
        }
     } else {
        echo "&lt;p&gt;" . $value . "&lt;/p&gt;\n";
     }
  }
  ?&gt;
  </code></pre>
  
  <p>output after submit:</p>
  
  <pre><code>blank white screen
  </code></pre>
  
  <p>Does this mean that I am trying to pass empty values to my db?  And I am no longer receiving all 0s in my database as I know have a SQL error (or is that because I am passing blank values ,i.e. $_POST[locationp]  and $_POST[searchtimep] are empty? is that the right conclusion? if so, why is it doing that?) with the updated PHP without single quotes; With the latest PHP [as seen above without single quotes, so it doesn't send as text] and the HTML above that now uses an ID tag for the form, I recieve:</p>
  
  <pre><code>"Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,)' at line 3" 
  </code></pre>
  
  <p>when I ty to submit from the webpage. And it follows that Chrome is not displaying anything regarding form values, only "Request Headers" and "Response Headers" are listed.</p>
  
  <p>My original question/post is still:  storing values in a MySQL db from a drop down menu?</p>
  
  <p>A related question that I was unable to glean a solution from: <a href="https://stackoverflow.com/questions/8000648/inserting-data-from-dropdown-into-database-with-php">Inserting Data from dropdown into database with PHP</a> </p>