Question

Scenario : The administrator is to assign different companies to different student. Problem : All student is given the same company of the last student in the form.

How do i make my hidden input work so that the correct selected drop down values of companies for each estudent to reflect correctly on the 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>";

The php form action :

$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';");
Was it helpful?

Solution

Try this:

//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
}

In your 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]."';");
   }
}

In this all your values will be updated (whether you changed any dropdown or not). If you want to limit only the changed dropdowns to be updated then you can have a hidden input variable which changes its value on change of dropdown and update your query only if the hidden input matches.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top