下拉列表未在编辑表单中加载或未根据 table id 选择行

dropdown list not loaded in edit form or not selected row according table id

我下面的代码是数据库中的编辑行。除了从数据库动态加载的两个下拉列表外,一切正常。但在编辑模式下,它没有加载列表中的所有选项或未根据数据库的行 ID 选择默认行 table。

请告诉我下面的代码需要做哪些更改。

我的代码:

<?php
//include database connection file
require_once'functions.php';
// Get the userid
$userid=intval($_GET['id']);
$onerecord=new DB_con();
$sql=$onerecord->fetchonerecord($userid);
$cnt=1;
while($row=mysqli_fetch_array($sql))
  {
 
  ?>
<?php include('header.php');?>
<body>

<div class="wrapper">
    <?php include('sidebar.php');?>

    <div class="main-panel">
        <nav class="navbar navbar-default navbar-fixed">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navigation-example-2">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#">Welcome</a>
                </div>
                <div class="collapse navbar-collapse">
                   

                    <ul class="nav navbar-nav navbar-right">
                       
                        <!-- <li class="dropdown">
                              <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                    <p>
                                        Dropdown
                                        <b class="caret"></b>
                                    </p>

                              </a>
                              <ul class="dropdown-menu">
                                <li><a href="#">Action</a></li>
                                <li><a href="#">Another action</a></li>
                                <li><a href="#">Something</a></li>
                                <li><a href="#">Another action</a></li>
                                <li><a href="#">Something</a></li>
                                <li class="divider"></li>
                                <li><a href="#">Separated link</a></li>
                              </ul>
                        </li> -->
                        <li>
                            <a href="#">
                                <p>Log out</p>
                            </a>
                        </li>
                        <li class="separator hidden-lg hidden-md"></li>
                    </ul>
                </div>
            </div>
        </nav>

<!-- content area -->
        <div class="content">
            <div class="container-fluid">
                <div class="row">
 <!-- Form Start -->
 
 <div class="col-md-10 " style="float:none; margin:0 auto;">
                        <div class="card">
                            <div class="header">
                                <h4 class="title">Edit Staff</h4>
                                <a href="allstaff.php" class="btn btn-info btn-fill pull-right " style="margin:-30px 0 0 0 ;" >All Staff</a>
                            </div>
                            <div class="content">
                                <form action="update.php" method="post">
                                     <div class="row">  <!-- Row 1 Form  -->
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label>Name</label>
                                                <input type="text" name="name" class="form-control" placeholder="Enter Name" value="<?php echo htmlentities($row['name']);?>">
                                                <input type="hidden"  name="id" value="<?php echo htmlentities($row['id']);?>" >
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label>Email</label>
                                                <input type="email" name="email" class="form-control" placeholder="Username" value="<?php echo htmlentities($row['email']);?>">
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label for="exampleInputEmail1">Phone</label>
                                                <input type="text" class="form-control" name="phone" placeholder="Enter Phone" value="<?php echo htmlentities($row['phone']);?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">  <!-- Row 2 Form  -->
                                    <div class="col-md-4">
                                            <div class="form-group">
                                                <label for="exampleInputEmail1">EMP ID</label>
                                                <input type="text" class="form-control" placeholder="Enter EMP ID" name="empid" value="<?php echo htmlentities($row['empid']);?>">
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label>Department</label>
                                                <select data-placeholder="Select Department" name="dept" id="" class="form-control select2" >
                                             <!-- <option value="">Select Department</option> -->
                                             
                                             <option  value="<?php if($row['dept']=="Graphics") echo 'selected="selected"';?>"><?php echo htmlentities($row['dept']);?></option>
                                             
                                             
                                             
                                            </select>
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label>Designation</label>
                                                <select data-placeholder="Select Department" name="desi" id="" class="form-control select2" >
                                             <!-- <option value="">Select Designation</option> -->
                                             
                                             <option  value="<?php echo htmlentities($row['desi']); echo 'selected="selected"';?>"><?php echo htmlentities($row['desi']);?></option>
                                            
                                         
                                            
                                             
                                            </select>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <div class="col-md-8">
                                            <div class="form-group">
                                                <label>Address</label>
                                                <input type="text" class="form-control" placeholder="Enter Address" name="address" value="<?php echo htmlentities($row['address']);?>">
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label>City</label>
                                                <input type="text" class="form-control" placeholder="Enter City" name="city" value="<?php echo htmlentities($row['city']);?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label>State</label>
                                                <input type="text" class="form-control" placeholder="Enter State" name="state" value="<?php echo htmlentities($row['state']);?>">
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label>Pin Code</label>
                                                <input type="text" class="form-control" placeholder="Enter Pin Code" name="pcode" value="<?php echo htmlentities($row['pcode']);?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                    <div class="col-12">
                                    <hr>
                                    </div>
                                    </div>
                                    <div class="row">
                                    <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="">Date of Joining</label>
                                        <input type="date"  value="<?php echo strftime('%Y-%m-%d', strtotime($row['doj']));?>"  placeholder="Enter Date of Joining" name="doj" id="date1" class=" form-control ">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="">PAN Card</label>
                                        <input type="text" value="<?php echo htmlentities($row['pan']);?>"  placeholder="Enter PAN Card" name="pan" id="" class="form-control ">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="">Adhaar No.</label>
                                        <input type="text" value="<?php echo htmlentities($row['adhaar']);?>"  placeholder="Enter Adhaar No." name="adhaar" id="" class="form-control ">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-12"><hr></div>
                                <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="">Bank Name</label>
                                         <input type="text" value="<?php echo htmlentities($row['bname']);?>" name="bname" placeholder="Enter Bank Name." id="" class="form-control ">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="bank_account_no">Bank Account No.</label>
                                         <input type="text" name="bacc" value="<?php echo htmlentities($row['bacc']);?>" placeholder="Enter Bank Account No.." id="" class="form-control ">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="ifsc">IFSC Code.</label>
                                         <input type="text" value="<?php echo htmlentities($row['ifsc']);?>" name="ifsc" placeholder="Enter IFSC Code." id="" class="form-control ">
                                    </div>
                                </div>  
                                </div>       <!-- Row End -->
                                   
                                       
                                <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="ifsc">Account Type</label>
                                        <select required="" name="acct" value="" data-placeholder="Select Account Type" id="" class="form-control select2">
                                            <option value=""></option>
                                            <option  value="1" <?php if($row['acct']=="1") echo 'selected="selected"'; ?>>Saving Account</option>
                                            <option  value="2" <?php if($row['acct']=="2") echo 'selected="selected"'; ?>>Current Account</option>
                                            <option  value="3" <?php if($row['acct']=="3") echo 'selected="selected"'; ?> >Salary Account</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="ifsc">Monthly Salary</label>
                                         <input type="text" value="<?php echo htmlentities($row['msal']);?>" name="msal" placeholder="Enter Monthly Salary" id="" class="form-control ">
                                    </div>
                                </div>
        
                                </div>       <!-- Row End -->
                                <?php } ?>  

                                    <button type="submit" class="btn btn-info btn-fill pull-right" name="save">Update</button>
                                    <div class="clearfix"></div>
                                </form>
                            </div>
                        </div>
                    </div>
<!-- Form End -->

                    
                    
                </div>
            </div>
        </div> <!-- Content area end -->


        <footer class="footer">
            <div class="container-fluid">
                
                
            </div>
        </footer>

    </div>
</div>


</body>

    <!--   Core JS Files   -->
    <script src="assets/js/jquery.3.2.1.min.js" type="text/javascript"></script>
    <script src="assets/js/bootstrap.min.js" type="text/javascript"></script>

    <!--  Charts Plugin -->
    <script src="assets/js/chartist.min.js"></script>

    <!--  Notifications Plugin    -->
    <script src="assets/js/bootstrap-notify.js"></script>

    <!--  Google Maps Plugin    -->
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY_HERE"></script>

    <!-- Light Bootstrap Table Core javascript and methods for Demo purpose -->
    <script src="assets/js/light-bootstrap-dashboard.js?v=1.4.0"></script>

    <!-- Light Bootstrap Table DEMO methods, don't include it in your project! -->
    <script src="assets/js/demo.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>
<script>
        jQuery(document).ready(function($) {
            $('form').parsley();
            CKEDITOR.replaceAll( 'cke' );  
            $('.select2').select2(); 
            $( ".date" ).datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat:'dd-M-yy',
        });
            $( ".date2" ).datepicker({
            changeMonth: true,
            changeYear: true,
            maxDate:0,
            dateFormat:'dd-M-yy',
        });
 
 
        });
    </script>
   
</html>

一些步骤应该是可行的:

  1. 在 select

    添加 selected
     <div class="form-group">
     <label>Department</label>
     <select selected data-placeholder="Select Department" name="dept" id="" class="form-control select2" >                  
    <option  value="<?php if($row['dept']=="Graphics") echo 'selected="selected"';?>"><?php echo htmlentities($row['dept']);?></option>                                             
      </select>
     </div>
    
  2. 如果还是报错在options中修改逻辑

     <div class="form-group">
     <label>Department</label>
     <select selected data-placeholder="Select Department" name="dept" id="" class="form-control select2" >
    <option <?php echo ($row['dept'] == "Graphics")?'selected':''; ?> value="<?php $row['dept'];?>"><?php echo htmlentities($row['dept']);?></option> 
     </select>
     </div>
    

3.Check 再次使用 var_dump 也许 $row['dept'] 还没有得到值

顺便说一句,我通常使用带有循环逻辑的上述源代码