duanlu5055
duanlu5055
2015-07-12 14:37

在刷新页面之前,MySql数据不会显示在html表单上

已采纳

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>");
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dounangshen6553 dounangshen6553 6年前

    After considerable time bashing the code around I have found a solution, this may be ugly and there may be more efficient ways of doing this however here is the final piece of code that works for me

    //editing departments using department id//
                $select_o_dept_names = "SELECT *
                    FROM `departments` 
                    WHERE department_id = ".$main_report['department']."";              
                $run_select_o_dept = mysql_query($select_o_dept_names,$link); 
                $dept_o_data=mysql_fetch_array($run_select_o_dept);
    
                $select_dept_names = "SELECT *
                    FROM `departments` 
                    WHERE status = 'active' 
                    ORDER BY department_name ASC ";             
                $run_select_dept = mysql_query($select_dept_names,$link); 
    
                print("<table id='edit_table_within'>Department:</td><td><select name=\"department\"> 
                 <option value=\"".$main_report['department']."\">".$dept_o_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>");
    
    点赞 评论 复制链接分享
  • doumen5895 doumen5895 6年前

    If i'm understanding you well enough(and I'm probably not) I would recommend adding a peice of code that automatically refreshes the page when you confirm that data has been added or changed into database. Also you have a blank box in dropdown boxes because you are selecting a default option. Hope I'm understanding your question

    点赞 评论 复制链接分享