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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?