I have attempted to research this one and have also tried a number of fixes, all ending up a mess. I am novice with this and am aware I am using a deprecated version of PHP, code is not my job however I am stuck with fixing this for work because I will benefit by reducing errors and getting other people on board to maintain data. I am changing a PHP / MySQL application that was once all full of hard coded select boxes, I have created a small html form to CRUD a set of department names in a table called 'departments' and am now cross referencing this across the site. The mysql 'department' table has 3 fields department_id, status, department_name
The form I am having trouble with updates a table called 'staff' it allows to CRUD this data in the staff table however on page load of the form it does not populate the department name it shows an empty select box. The relevant field from the 'staff' table is staff_company. I am able to open up a drop down box which is populated with all the correct options, if i leave the box untouched and update other data on the form it posts the original correct department_id maintaining the integrity of the data and on refreshing the page the correct department name does show. It is an issue if anyone else uses it as they may think that the field is empty when they try to edit and may make an incorrect selection, it is also sloppy, my thoughts are that I am somehow not declaring the variable early enough any suggestions in novice talk / beginner talk will be appreciated part of code below - plus I need to understand where I am going wrong so I can improve my skills (note page approximately 1600 lines therefore not posting whole page).
//editing departments using department id//
$select_dept_names = "SELECT *
FROM `departments`
WHERE status = 'active'
ORDER BY department_name ASC ";
$run_select_dept = mysql_query($select_dept_names,$link);
print(" <tr><td id='employee_left'>Department:</td><td><select name=\"staff_company\">
<option value=\"".$grab_staff['staff_company']."\">".$dept_data['department_name']."</option>");
while($dept_data=mysql_fetch_array($run_select_dept))
{
print("<option value=\"".$dept_data['department_id']."\">".$dept_data['department_name']."</option>");
}print("
</select></td></tr>");
Many thanks in advance - Cheers Jase
Updated code below
//line 346 queries
$select_dept_names = "SELECT * FROM departments
ORDER BY department_name ASC ";
// line 363 - storing and fetching data
$run_select_dept = mysql_query($select_dept_names,$link);
$dept_data = mysql_fetch_array($run_select_dept);
// line 1147 - editing departments using department id///////////NEEDS WORK Only showing first department then on F5 displays existing department
//var_dump($main_report[department]); die(); - outputs only one line from the 'department' table
if($main_report['department'] = $dept_data['department_id'])
{
print(" <table id='edit_table_within'>Department:</td><td> <select name=\"department\">
<option value=\"".$main_report['department']."\">".$dept_data['department_name']." </option>");
}
else print("<table id='edit_table_within'>Department:</td><td> <select name=\"department\"> <option value=\"\">Select..</option>");
while($dept_data=mysql_fetch_array($run_select_dept))
{
print("<option value=\"".$dept_data['department_id']."\">".$dept_data['department_name']." </option>");
}print("
</select></td></tr>");