简体   繁体   中英

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.

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.

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

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

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:

$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. When submitting the form you can just update the values of the current row by using 'id' in your MySQL query.

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

First of all, I think you should check into using object oriented MySQL commands. You're doing this the pain-staking way.

Check out either an ORM DB layer or at least use 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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