douchengchu8374
2014-02-10 10:29
浏览 28
已采纳

将值插入数据库输入或下拉菜单

I have a form that a user can insert his/her level of education either by picking a value from a drop down menu or just typing some text. My code is simple:

<label for="education">Education: <input type="text" name="education"/> </label> 
<select name="education">
   <option value=""></option>
   <option value = "Primary education" name="Primary education">Primary education</option>
   <option value = "Secondary education" name="Secondary education">Secondary education</option>
   <option value = "Bachelor" name="Bachelor">Bachelor</option>
   <option value = "Master" name="Master">Master </option>
   <option value = "Doctoral" name="Doctoral">Doctoral </option>
</select>

So i want to insert that value in a column called education in database. Using code above the value is inserted only when someone is picking a value from menu. Input text is not stored in database.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxian6086 2014-02-10 10:34
    已采纳

    In your textbox part;

    Change

    <label for="education">Hobby: <input type="text" name="education"/> </label>
    

    to

    <label for="education">Hobby: <input type="text" name="education_text"/> </label>
    

    Duplicate name causes your problem(select and textbox name are same). I have given education_text as an example, update it according to your needs. Do not forget to handle it backend with updated name.

    On your backend;

    $education = "";
    if(!empty($_REQUEST["education_text"])) {
       $education = $_REQUEST["education_text"];
    } else if(!empty($_REQUEST["education"])) {
       $education = $_REQUEST["education"];
    } else {
       die("Invalid education");
    }
    

    And use it in your query. You can change $_REQUEST to $_POST or $_GET according to your needs

    点赞 评论

相关推荐 更多相似问题