簡體   English   中英

如何將下拉列表中的其他選定值插入數據庫

[英]How to insert different selected value from drop down list into database

場景:管理員將不同的公司分配給不同的學生。 問題:所有學生的表格都與上一個學生的同一個公司。

如何使隱藏的輸入起作用,以便正確選擇每個公司的下拉列表值以正確反映數據庫?

$result = mysqli_query($con,"SELECT student_id, admin_no, name, GPA, gender FROM student_details WHERE jobscope1= 'Information Technology' ORDER BY `GPA` DESC; ");
$result2 = mysqli_query($con,"SELECT job_title FROM job_details WHERE jobscope='Information Technology' ORDER BY `job_title` ASC;");

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

            //return the array and loop through each row
            while($row = mysqli_fetch_assoc($result))
            {


            $adminno = $row['admin_no'];
            $name = $row['name'];
            $gpa = $row['GPA'];
            $gender = $row['gender'];

                  echo "<tr>";
                  echo '<input type=hidden name=admin_no value='. $adminno . '/>';
                  echo "<td>" . $adminno . "</td>";
                  echo "<td>" . $name . "</td>";
                  echo "<td>" . $gpa . "</td>";
                  echo "<td>" . $gender . "</td>"; 
                  echo "<td><select name='ddl'  { myform.submit('') }'>".$options."</select></td>";
              }
                  echo "</tr>";

PHP表單動作:

$query = mysqli_query($con, "SELECT * FROM student_details WHERE jobscope1 = 'Information Technology';");
while ($row = mysqli_fetch_assoc($query)) 

  $complist = $_POST['ddl'];

$result4 = mysqli_query($con, "UPDATE `student_details` SET `company`= '" . $complist . "' WHERE `jobscope1` = 'Information Technology';");

嘗試這個:

//return the array and loop through each row
while($row = mysqli_fetch_assoc($result))
{

    $adminno = $row['admin_no'];
    $name = $row['name'];
    $gpa = $row['GPA'];
    $gender = $row['gender'];

    echo "<tr>";
    //changed here (this is called an input array which makes it hold multiple 
    //values with same name)
    echo "<input type='hidden' name='admin_no[]' value='". $adminno ."'/>"; //edited
    echo "<td>" . $adminno . "</td>";
    echo "<td>" . $name . "</td>";
    echo "<td>" . $gpa . "</td>";
    echo "<td>" . $gender . "</td>"; 
    //changed here too
    echo "<td><select name='ddl[]' >".$options."</select></td>"; //edited
}

在您的PHP中:

if(isset($_POST['ddl'])){
   foreach( $_POST['ddl'] as $index => $val ) //edited extra { here
   {
     $result4 = mysqli_query($con, "UPDATE `student_details` 
                                    SET `company`= '" . $val . "' 
                                    WHERE `jobscope1` = 'Information Technology' 
                                    AND `admin_no` = '".$_POST['admin_no'][$index]."';");
   }
}

在此,您的所有值將被更新(無論您是否更改了任何下拉菜單)。 如果只想限制已更改的下拉列表以進行更新,則可以使用一個隱藏的輸入變量,該變量將在更改下拉列表時更改其值,並且僅在隱藏的輸入匹配時才更新查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM