使用按钮更新表中的特定行 [英] Update a specific row in a table using a button

查看:54
本文介绍了使用按钮更新表中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html表,其中显示了数据库中的所有条目,因此每个条目都有一行.

I have a html table that displays all the entries in my db, so I have one row for each entry.

每行都有一个按钮.

单击按钮时,我要编辑与按下的按钮关联的行的值,并获取与该特定行有关的所有值.

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?

推荐答案

您可以在每个按钮集周围放置一个表单,并添加一个包含要更新的字段ID的隐藏字段.

You can put a form around every button set and add a hidden field containing the id of the field to update.

您还需要删除包装表格的表单标签.

You also would need to remove the form tag that wraps the table.

现在只有一个ID和按下的按钮将被提交.

Now only one ID and the pressed button will get submitted.

在表中:

<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>

稍后在php中:

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

这篇关于使用按钮更新表中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆