更新表行中的数据[PHP]

时间:2017-09-13 11:21:46

标签: php html mysql

我有一个标准的HTML表,表中的每一行都是使用循环从数据库表生成的。

在每行的末尾,我有一个更新按钮,我希望更新表格字段中的数据。

下图显示了这个概念。

enter image description here

表格本身

<div class="container" id="users">

    <div class="row">
     <div class="col-md-12">
      <div class="table-responsive">

        <form method="post" action="">

            <table class="table table-bordered table-hover table-striped tablesorter">
             <thead>

                <tr>

                 <th width="10%" style="text-align:left">Forename</th>
                 <th width="15%" style="text-align:left">Surname</th>
                 <th width="35%" style="text-align:left">Email</th>
                 <th width="30%" style="text-align:left">Permissions</th>
                 <th width="5%" style="text-align:left">Update</th>
                 <th width="5%" style="text-align:left">Delete</th>

               </tr>
             </thead>
            <tbody>
               <tr>
                <!--here showing results in the table -->
            <?php  

                 $adminquery = "SELECT * FROM admin ORDER by user_id DESC";  
                 $IDlist = mysqli_query($dbconEDB, $adminquery);

                 while($rowlist=mysqli_fetch_array($IDlist))//while look to fetch the result and store in a array $row.  
                        { 
                        $user_id = $rowlist["user_id"]; 
                        $admin_email = $rowlist["admin_email"]; 
                        $forename = $rowlist["forename"];
                        $surname = $rowlist["surname"];

                        $JPortal = $rowlist["JPortal"];
                        $Tenders = $rowlist["Tenders"];
                        $News= $rowlist["News"];
                        $Events = $rowlist["Events"];
                        $Users= $rowlist["Users="] ;

                           ?>

                    <td style="text-align:left">
                     <div class="form-group">
                      <input name="user_id" id="user_id" type="text"  class="form-control" value="<?php echo $user_id;?>">
                      <input name="forename" id="forename" type="text"  class="form-control" value="<?php echo $forename;?>">
                      <div class="hidden">  <?php echo $forename;?></div>
                     </div>
                    </td>

                    <td style="text-align:left">
                      <div class="form-group">
                        <input name="forename" id="surname" type="text"  class="form-control" value="<?php echo $surname;?>">
                         <div class="hidden">  <?php echo  $surname;?></div>
                      </div>
                    </td>

                    <td style="text-align:center">
                     <div class="form-group">
                       <input name="admin_email" id="admin_email" type="text"  class="form-control" value="<?php echo $admin_email;?>">
                       <div class="hidden">  <?php echo  $admin_email;?></div>
                     </div>
                    </td>

                  <td style="text-align:center">


                        <label>
                          <input name="JPortal" type="checkbox" id="JPortal"   value="1" <?php if ($JPortal == 1) echo "checked='checked'" ;?>> Jobs 
                        </label>

                        <label>
                         <input type="checkbox" name="Tenders" value="1" id="Tenders"   <?php if ($Tenders == 1) echo "checked='checked'" ;?>> News 
                        </label>

                        <label>
                         <input type="checkbox" name="News" value="1" id="News"   <?php if ($News == 1) echo "checked='checked'" ;?>> Tenders 
                        </label>

                        <label>
                         <input type="checkbox" name="Events" value="1" id="Events"  <?php if ($Events == 1) echo "checked='checked'" ;?>> Events 
                        </label>

                        <label>
                         <input type="checkbox" name="Users" value="1" id="Users"   <?php if ($Users == 1) echo "checked='checked'" ;?>> Users 
                        </label>


                   </td>                

                  <td style="text-align:center">

                        <input class="btn btn-newable " type="submit" value="Update" name="EDBsubmit">
                  </td>

                  <td style="text-align:center">
                     <a href="users.php?user=<?php echo $user_id; ?>"><button class="btn btn-newable">update2</button></a>
                  </td>

            </tr>
                        <?php } ?>

              </tbody>
            </table>

         </form>

       </div>
      </div>

    </div>

我以为我只是休息了一天,实际上可以将表格包裹在桌子的每一排。

1 个答案:

答案 0 :(得分:0)

一种简单的方法是在每个TD中包含一个包含更新按钮的表单。 只需将此按钮设为输入[type = submit],然后在此表单中添加输入[type = hidden],其中包含行行的ID。然后,您基本上可以在$ _POST中获取ID。

示例:

<td class="actions">
    <form method="post">
        <input type="hidden" name="row_id" value="<?php echo $line['id']; ?>"/>
        <input type="submit" value="Update"/>
    </form>
</td>