2017-09-13 75 views
1

我有一個標準的HTML表格,表格中的每一行都是使用循環從數據庫表格生成的。更新表格行中的數據[PHP]

在每一行的末尾,我有一個更新按鈕,我希望這可以更新表字段中的數據。

下圖顯示了這個概念。

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> 

我想我只是有一個休息日,並可能在事實上纏表的每一行的形式。

+0

請到目前爲止添加您嘗試的代碼段。 Stackoverflow不僅僅是人們爲你做所有工作的地方。 –

+0

好吧:)現在就做。 –

+0

爲什麼我的評論被刪除? – Script47

回答

0

一個簡單的方法是在每個包含更新按鈕的TD中都有一個表單。 只需將此按鈕設爲輸入[type = submit],然後在包含您行ID的此表單中添加一個輸入[type = hidden]。然後,你可以基本上在$ _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> 
+0

是的,我的腦子裏肯定過於複雜 –