简体   繁体   English

使用按钮更新表中的特定行

[英]Update a specific row in a table using a button

I have a html table that displays all the entries in my db, so I have one row for each entry. 我有一个html表,该表显示数据库中的所有条目,因此每个条目都有一行。

I have also a button for each row. 每行都有一个按钮。

When I click the button, I want to edit the values of the row associated to the pressed button, and take all the values related to that specific row. 当我单击按钮时,我要编辑与按下的按钮关联的行的值,并获取与该特定行有关的所有值。

<html>
<body>
<?php

$user="user";
$password="password";
$database="database";

$username=$_SESSION['username'];

mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM table";
$result=mysql_query($query);
$num=mysql_numrows($result);

?>


<form id="view_admin" method="post">


<table id="my_table" class= "sample" >

<tr>
<td align="center"><strong><font face="Arial, Helvetica, sans-serif">Id</font></strong></td>
<td align="center"><strong><font face="Arial, Helvetica, sans-serif">Column1</font></strong></td>
<td align="center"><strong><font face="Arial, Helvetica, sans-serif">Status</font></td>
<td align="center"><strong><font face="Arial, Helvetica, sans-serif">Edit Status</font></td>
</tr>

<?php

$i=0;
while ($i < $num) {

$f0=mysql_result($result,$i,"id");
$f1=mysql_result($result,$i,"column1");
$f2=mysql_result($result,$i,"status");

?>

<tr>
<td align="center"><font face="Arial, Helvetica, sans-serif"><?php echo $f0; ?></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td align="center"><input type="submit" name="approved" value="approve"> <input type="submit" name="refused" value="refuse"> </td>
</tr>

<?php
if (isset($_POST['approved'])) {
$query_update = "UPDATE main SET status='APPROVED' WHERE id ='$f0'";
$result_update=mysql_query($query_update);}
else if (isset($_POST['refused'])) {
$query_update = "UPDATE main SET status='REFUSED' WHERE id ='$f0'";
$result_update=mysql_query($query_update);
}
$i++;
}

mysql_close();


?>

</table>
</form>
</body>
</html>

Now, when I press the button, all the rows are updated. 现在,当我按下按钮时,所有行都会更新。

How can I solve my problem? 我该如何解决我的问题?

You can put a form around every button set and add a hidden field containing the id of the field to update. 您可以在每个按钮集周围放置一个表单,并添加一个包含要更新的字段ID的隐藏字段。

You also would need to remove the form tag that wraps the table. 您还需要删除包装表格的表单标签。

Now only one ID and the pressed button will get submitted. 现在只有一个ID和按下的按钮将被提交。

In the table: 在表中:

<form id="view_admin" method="post">
  <input type="hidden" name="f0" value="<?php echo $f0; ?>">
  <input type="submit" name="approved" value="approve">
  <input type="submit" name="refused" value="refuse">
</form>

In the php later on: 在以后的php中:

$f0_submitted = (int) $_POST['f0'];
$query_update = "UPDATE main SET status='APPROVED' WHERE id ='$f0_submitted'";

You may use one form tag for each row and include a hidden field 'id' for each row. 您可以为每一行使用一个表单标签,并为每一行添加一个隐藏字段“ id”。 When submitting the form you can just update the values of the current row by using 'id' in your MySQL query. 提交表单时,您只需在MySQL查询中使用“ id”来更新当前行的值。

UPDATE table SET ... WHERE id = $_POST['id']

First of all, I think you should check into using object oriented MySQL commands. 首先,我认为您应该检查使用面向对象的MySQL命令。 You're doing this the pain-staking way. 您正在为此付出艰苦的努力。

Check out either an ORM DB layer or at least use mysqli. 检出ORM DB层或至少使用mysqli。 http://www.php.net/manual/en/mysqli.quickstart.statements.php http://www.php.net/manual/en/mysqli.quickstart.statements.php

The reason you are updating every row is because you have one big form with a bunch of buttons with the same name. 更新每一行的原因是因为您有一个很大的表单,其中有一堆名称相同的按钮。 So clicking on any one of them is like clicking on all of them. 因此,单击它们中的任何一个都类似于单击所有它们。 Your conditional statement is true every loop. 您的条件语句在每个循环中都是正确的。

Instead, if you want to stick with doing this in PHP, I would make the submit name based on the id of the row. 相反,如果您想坚持使用PHP进行此操作,我将根据行的ID来确定提交名称。 Then when the page posts and loads again, if the posted value equals the current row id, make your update. 然后,当页面发布并再次加载时,如果发布的值等于当前行ID,请进行更新。

<form...>
  <input type='hidden' name='form_submitted' value='true' />
<?php
while(...)
{
  echo "<tr><td><input type='submit' name='update_{$row_id}' value='Update' /></td></tr>";
  if(!empty($_POST['form_submitted']) && isset($_POST['update_'.$row_id])) ...do update;
}
>?

You can go this route if you just want one big form, or you could do a form for each row with hidden types and values corresponding to row id. 如果您只想要一个大表格,则可以采用此方法,也可以为每行创建一个表格,其中包含与行ID对应的隐藏类型和值。

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

相关问题 使用表格中的按钮删除该特定行 - Using a button in a table to remove that specific row aJax使用按钮更新sqlite和php中的特定行 - aJax update a specific row in sqlite and php using a button 如何使用 javaScript 在按钮单击时从表中删除特定行 - how to delete specific row from table on button click using javaScript 更新多个条目表的特定行 - Update specific row of table of multiple entries 通过单击相应行上的按钮来获取特定的表行值 - Get specific table row values by clicking a button on the respective row 使用jQuery遍历表格,使用选中的复选框连续更新特定单元格 - Loop through table using jQuery, update specific cell in a row with a checked Checkbox 使用javascript单击按钮后如何禁用特定表行中的复选框? - How to disable checkbox in a specific table row after clicking a button using javascript? 仅使用JavaScript从JSON提取的表格数据中单击按钮时引用特定行 - Refer specific row on click of button in table data fetched from JSON using only JavaScript 使用角度从弹出模式按钮中删除时,删除表中的特定选定行 - Delete a specific selected row in a table when deleting from a pop-up modal button using angular 如何使用 reactjs 和材料 ui 将文本字段添加到按钮的表 onclick 内的特定行 - How to add a textField to a specific row inside a table onclick of a button using reactjs & material ui
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM