duanlu5055 2015-07-12 14:37
浏览 67
已采纳

在刷新页面之前,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 2015-07-23 03:52
    关注

    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>");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算