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条)

相关推荐 更多相似问题