javascript 使用按钮更新表中的特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13521746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update a specific row in a table using a button
提问by user1836941
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?
我该如何解决我的问题?
回答by jeroen
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'";
回答by mixable
You may use one form tag for each row and include a hidden field 'id' for each row. When submitting the form you can just update the values of the current row by using 'id' in your MySQL query.
您可以为每一行使用一个表单标签,并为每一行包含一个隐藏字段“id”。提交表单时,您可以通过在 MySQL 查询中使用“id”来更新当前行的值。
UPDATE table SET ... WHERE id = $_POST['id']
回答by Ryan
First of all, I think you should check into using object oriented MySQL commands. You're doing this the pain-staking way.
首先,我认为您应该检查使用面向对象的 MySQL 命令。你这样做是痛苦的。
Check out either an ORM DB layer or at least use mysqli. http://www.php.net/manual/en/mysqli.quickstart.statements.php
查看 ORM DB 层或至少使用 mysqli。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. Then when the page posts and loads again, if the posted value equals the current row id, make your update.
相反,如果您想坚持在 PHP 中执行此操作,我将根据行的 id 制作提交名称。然后当页面发布并再次加载时,如果发布的值等于当前行 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。