简体   繁体   中英

how to insert data from dropdown list to database

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>

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 , eg

$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.

For example I have the following:

<form action="phpfile.php" method="POST">
    <select name="color">
      <option value="volvo">Red</option>
      <option value="saab">Blue</option>
    </select>
    <input type="submit" />
</form>

This would be referenced as $_POST['color'] and would return the value of the selected

You would then run your insert query

$color = $_POST['color'];
$sql_add = "INSERT INTO example (`color`) VALUES ('".$color."')";

Make precautions to sanitize your input as well.

From What I understand in your question are you trying to display details of student in the table?

If you want to use $_POST change the form action to current page.

    $value_of_select_box = $_POST['SELECT BOX NAME HERE'];
    $result = mysqli_query($con,'SELECT admin_no, name, GPA, gender 
         FROM student_details 
                 WHERE jobscope1= "'.$value_of_select_box.'" ORDER BY GPA DESC; ');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM