store drop down options in mysql database, PHP

混江龙づ霸主 提交于 2019-12-13 00:34:36

问题


I need to store the user selected option when chosen from my drop down select box, and when submitted in my form, store in my MYSQL database.

I already have information being stored and displayed fine, i am just trying to add more content now. I have tried using the same method, but i cant get it to store what i want.

The select drop down looks like this inside my form:

  <span> Difficulty: </span> 
   <select>
              <option type="text" name="easy" id="easy" value="easy">easy</option>
              <option type="text" name="comfortable" id="comfortable" value="comfortable">comfortable</option>
              <option type="text" name="hard" id="hard" value="hard">hard</option>
              <option type="text" name="veryhard" id="veryhard" value="veryhard">very hard</option>
              <option type="text" name="hardest" id="hardest" value="hardest">Hardest ride in the world!</option>
            </select>
  </label>

and i am trying to send it using this:

    $difficulty = mysql_real_escape_string($_POST['difficulty']);

Using this query:

$query = sprintf("INSERT INTO markers (difficulty) VALUES ('%s')", $difficulty);

When my table is set up as INT, 20, allow null, it stores 0 in the database. and when i set it up VARCHAR, 20, allow null, it stores a blank field.

i want it to store whichever value the user chooses, i.e easy, hard, very hard etc.

Thanks very much


回答1:


$_POST['difficulty']

Would not hold a value as the SELECT has not been given a 'name'. Try

<select name="difficulty">



回答2:


Your problem is the name attribute in the HTML. It should be 'difficulty' for the select tag and not be in the option tags.

You also don't need type="text" as that is the default anyway. I suspect you don't need the id's either but of course we can't see the rest of the code so you might use them elsewhere.

<span> Difficulty: </span> 
<select name="difficulty">
  <option value="easy">easy</option>
  <option value="comfortable">comfortable</option>
  <option value="hard">hard</option>
  <option value="veryhard">very hard</option>
  <option value="hardest">Hardest ride in the world!</option>
</select>



回答3:


<select name="difficulty">
    <option type="text" id="easy" value="easy">easy</option>
    <option type="text" id="comfortable" value="comfortable">comfortable</option>
    <option type="text" id="hard" value="hard">hard</option>
    <option type="text" id="veryhard" value="veryhard">very hard</option>
    <option type="text" id="hardest" value="hardest">Hardest ride in the world!</option>
</select>

You don't give individual <option/> tags a name attribute.




回答4:


You need to change the name for HTML <select name="difficulty">. And then you will have in $_POST['difficulty'] the choosen value.

http://www.w3schools.com/TAGS/tag_Select.asp



来源:https://stackoverflow.com/questions/5912394/store-drop-down-options-in-mysql-database-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!