简体   繁体   English

当我有动态表格时,能够在“按钮单击”上更新一行

[英]Able to UPDATE a row on 'Button Click' when I have a dynamic table

I require some help with updating a row in table when I've pressed a button. 按下按钮后,我需要一些帮助来更新表中的行。 At the moment I have a dynamic table, which fetches data from the database and posts it as a table. 目前,我有一个动态表,该表从数据库中获取数据并将其发布为表。 What I really want to do now, is to be able to UPDATE a row on button click. 我现在真正想要做的就是能够在单击按钮时更新一行。 Here is my code. 这是我的代码。

My database (table name: assignment): 我的数据库(表名:assignment):

| ID | CustomerID | Name | Address | Technician |

My PHP: 我的PHP:

<?php 
if(isset($_POST['customerButton'])){
    $ID = $row_Assignment['ID']; <--- //Not sure which ID to fetch, so it'll be unique for each customer  
    $user = $row_Users['username']; <--- // Name of the logged in user.

    mysql_query("UPDATE assignment SET technician = '$user' WHERE ID='$ID'"); <--- //Not sure what to put after 'WHERE'. 
}

?>

Here's a snippet of the dynamic table 这是动态表的一小段

<td><?php echo $row_Assignment['address']; ?></td>
<td>
<?php echo $row_Assignment['technician']; ?>

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

    <input type="submit" name="customerButton" id="customerButton" value="Add">

</form>
</td>

Thanks! 谢谢!

So you want to update a table full of inputs? 因此,您想更新一个充满输入的表吗? I mean you download the table then excel like modify it and save it back. 我的意思是您先下载表格,然后再像修改表格一样保存并保存回去。

Make your table to be full of inputs inside the tds, except for the id which should be in a hidden input then you will use an array to send this information when you post meaning for example 使您的表在tds内充满输入,除了id应该在隐藏输入中,然后在发布含义时将使用数组发送此信息,例如

  <td><input name='name[]'></td><td><input name='comments[]'></td>...etc 

this way when you post 这样,当您发布

   $_POST['name']=// a list of the values then you just have to update each row  so...

update with a foreach in your php 用您的PHP中的foreach更新

   foreach($_POST['name'] as $key=>$value){
      $ID = $_POST['id'][$key]; 
         $name = value;
         $comments = $_POST['comments'][$key];

          mysql_query("UPDATE assignment SET technician = '$name',comments='$comments' WHERE ID='$ID'");

   }

This approach will update the whole table 这种方法将更新整个表

Just set a hidden value of ID in your html like so 就像这样在HTML中设置ID的隐藏值

<td>
<?php echo $row_Assignment['technician']; ?>

<form action="" method="post"> 
    <input type="hidden" name="assignment_id" value="".$row_Assignment['ID']."">
    <input type="submit" name="customerButton" id="customerButton" value="Add">

</form>
</td>

Then on your php code for updating you could easily identify which row was submitted by getting the id as such 然后在您的php代码上进行更新,您可以通过获取ID来轻松确定提交了哪一行

$ID = $_POST['assignment_id'];

Then you can use that $ID in the where condition of the update and set whatever value you need to set for the columns you need to update. 然后,您可以在更新的where条件中使用该$ ID,并为需要更新的列设置所需设置的任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM