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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条