1

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';");
4
  • It's obvious many rows will be updated since you are not specifying in your update query for which student it should be updated for. Is student_id the primary key of student_details? Commented Feb 3, 2014 at 9:20
  • The admin_no is the primary key of student_details Commented Feb 3, 2014 at 9:33
  • One more question, currently is it updating as and when you pick an option from the dropdown? Or do you select values in multiple dropdowns and then submit? Commented Feb 3, 2014 at 9:40
  • @user007 We have to select values in multiple dropdowns and then submit. Commented Feb 3, 2014 at 9:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

edited the necessary changes howere nothing is reflected on the database now.
how do you use the hidden input match that you mention? @user007
Let's discuss here
i cant seem to discuss there... @user007
Alright never mind, could you please explain what happens when you submit the form? Any error messages shown? Does the page get refreshed? If it's only the update query not running, please try echo mysqli_error($con); after the update query;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.