0

I have a while loop that display data from table student and an extra column that displays a drop down selection from employee table id which when selected, updates the id in student table. I have the following codes but on displaying it shows only 1 data from the employer table each line.

Here is the while loop. Any help would be appreciated, Thanks in advance.

while(($row = mysql_fetch_array($search_result)) && ($row1 =mysql_fetch_array($search_result2))){
echo"<form action=manualallocation.php method=post>";
echo"<tr>";
echo "<td>" . $row['stud_ID']."</td>";
echo "<td>" . $row['stud_NAME']."</td>";
echo "<td>"."<Select name='ex1'>"."<option value='" .$row1['emp_id'] ."'>" .$row1['emp_id'] ."</option>"."</select>";
echo "<td>" . "<input type='char' name='emp_location' value='" .$row1['emp_location'] . "'/> </td>";
echo "<td>" . "<input type='char' name='stud_FIELDOFSTUDY' value='" .$row['stud_FIELDOFSTUDY'] . "'/> </td>";
echo "<td>" . "<input type='char' name='student_yearCompleted' value='" .$row['student_yearCompleted'] . "'/> </td>";
echo "<input type='hidden' name=hidden value=" . $row['stud_ID'] . ">";
echo "<td>" . "<input type='submit' name='submit' value=update". "></td>";
echo "</tr>";
echo "</form>";
}
Oliver
  • 23
  • 6

2 Answers2

0
while(($row = mysql_fetch_array($search_result)) && ($row = mysql_fetch_array($search_result2))){

Here you rewrite your $row variable with the second statement. You should use different variables.

while(($row_stud = mysql_fetch_array($search_result)) && ($row_emp = mysql_fetch_array($search_result2))){

And then when working with emp data use $row_emp

$row_emp['emp_id']

and when with students - $row_stud

$row_stud['stud_ID']    
ksimka
  • 1,394
  • 9
  • 21
0

1) Replace && with and. Check this link for documentation.

Watch out for the difference of priority between 'and vs &&' or '|| vs or':

<?php
$bool = true && false;
var_dump($bool); // false, that's expected

$bool = true and false;
var_dump($bool); // true, ouch!
?>

Because 'and/or' have lower priority than '=' but '||/&&' have higher.

2) Use two different variable in while loop $row1 and $row2. So first variable cannot overwrite second.

while(($row1 = mysql_fetch_array($search_result)) and ($row2 = mysql_fetch_array($search_result2)))

For your Dropdown, Check this links:-

Link1

Link2

Warning

mysql_* was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

Hope it will help you :)

Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • Thanks it works partly. For the drop down how can i take data from the employer table and updates it in student table when i select it and press update? – Oliver Mar 07 '16 at 12:13
  • @Oliver: You are welcome :-) I am request you to ask this question as a new one . – Ravi Hirani Mar 07 '16 at 12:15
  • check this link:- http://stackoverflow.com/questions/7695599/get-the-selected-index-value-of-select-tag-in-php – Ravi Hirani Mar 07 '16 at 12:18
  • When i copy paste the codes and change the values, it takes only 1 value from the table employer of emp_Name each lines. It does not give me the options on a single dropdown each line. Any idea why ? Here is the extracted code echo "" . "".""; – Oliver Mar 07 '16 at 13:15