dongxiz5342 2014-01-25 09:46 采纳率: 100%
浏览 69
已采纳

如何将数据从下拉列表插入数据库

Since I already have the data in the dropdown list, how do i reflect it in the database? The database should show the exact admin no, student, gpa and one of the option from the drop down list. Do I need to use an if else statement?

<form name="IT" action="getIT_now.php" method="post">     
        <?php
            $result = mysqli_query($con,"SELECT admin_no, name, GPA, gender FROM student_details WHERE jobscope1= 'IT' ORDER BY `GPA` DESC; ");

                $result2 = mysqli_query($con, "SELECT job_title FROM job_details WHERE jobscope='IT';");
                $row2 = mysqli_fetch_assoc($result2);

                echo "<table border='1' >
                <tr>
                <th>Admin Number</th>
                <th>Student Name</th>
                <th>GPA</th>
                <th>Gender</th>
                <th>Company List</th>
                </tr>";

                 /*options sections start*/
                $options= '';
                while ($row2 = mysqli_fetch_array($result2))
                {
                    $options .='<option value="'. $row2['job_title'] .'"> '. $row2['job_title'] .'</option>';
                }
                /*options sections end*/

                while($row = mysqli_fetch_assoc($result))
                  { 




                      echo "<tr>";
                      echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
                      echo "<td bgColor=white>" . $row['name'] . "</td>";
                      echo "<td bgColor=white>" . $row['GPA'] . "</td>";
                      echo "<td bgColor=white>" . $row['gender'] . "</td>"; 
                      echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'>".$options."</select></td>";
                  }
                      echo "</tr>";              

                      echo "</table>";



    ?>
    <input type="submit" name="submit" id="submit" value="Submit" />

    </form>
  • 写回答

3条回答 默认 最新

  • dongshetao1814 2014-01-25 10:18
    关注

    Change your form action to the name of the current file. Then at the top of your file open the following IF statement:

    if ($_SERVER['REQUEST_METHOD']=='POST'){
    

    In this IF statement you want to assign a variable to the value of your dropdown (you need to wrap your options in <select> tags, and the opening tag needs a name attribute) and write that variable to the database with an INSERT query. You do this with $_POST, e.g.

    $variable = $_POST['name_of_select'];
    

    You then do your INSERT:

    $query = "INSERT INTO table (field) VALUES ($variable)";

    (this is very generalised, and you should look into prepared statements as a priority, because you should never trust any user input, and prepared statements greatly increase security).

    Followed by a check that the INSERT was successful, etc. Then you can close your IF statement and add an ELSE. Inside that you can put your existing code.

    What this does is first check if the page has been POSTed. If it has, it runs the INSERT query. If not, it displays the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器