Forum Moderators: coopster

Message Too Old, No Replies

Deleting Rows From a Table - PHP

Deleting Rows From a Table - PHP

         

Imy_S3

7:15 pm on Mar 15, 2004 (gmt 0)

10+ Year Member



Hi

Got a delete button that deletes a row from the table.
For each row i have also put a button in to each row.

So when pressed it deletes that particular row.

however, what ever button is pressed, always the last row is deleted.

The code is below:

When the button is pressed it calls another script called deleteShoppingSelection.php using the:

print "<td><INPUT TYPE=hidden NAME=variableName VALUE=".$row["shopid"]." SIZE=5>";
print" <td><input type=submit value='Delete This Record'";
print" <td><input type=button value='Back' onClick='history.go(-1)'>";
print "\n";

it then deletes the row based on the shopid it was passed, but like i said always the last one.

how to i tell it that if the delete button on the 3rd row is pressed then delete that one.

Thanks in advance.

Timotheos

11:00 pm on Mar 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Imy_S3,

I think your problem is that you're using the hidden field to determine the shopid number but if the variable name is the same then it gets assigned the last one.

Maybe somebody has a better way but I would think it would be best just to provide a link (or maybe your own image link).

<a href="deleteShoppingSelection.php?shopid="<?php echo $row['shopid']?>">Delete</a>

Imy_S3

11:53 pm on Mar 15, 2004 (gmt 0)

10+ Year Member



i dont understand what you mean?

thanks

Timotheos

5:40 am on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What don't you understand... your problem or my alternative?

Imy_S3

11:07 am on Mar 16, 2004 (gmt 0)

10+ Year Member



dont understand your solution

<a href="deleteShoppingSelection.php?shopid="<?php echo $row['shopid']?>">Delete</a>

What is the above line doing

timster

3:26 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A problem with how the HTML form is set up may cause this problem.

If you HTML looks a little like this, if I'm not mistaken, the form will send the last value of "VALUE."

<form action="delete.php">
...
<INPUT TYPE=hidden NAME=variableName VALUE="1" SIZE=5>
<input type=submit value='Delete This Record'">
...
<INPUT TYPE=hidden NAME=variableName VALUE="2" SIZE=5>
<input type=submit value='Delete This Record'">
...
<INPUT TYPE=hidden NAME=variableName VALUE="3" SIZE=5>
<input type=submit value='Delete This Record'">
...
</form>

daisho

6:58 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



Or around each "Delete" button and hidden variable add <form></form> tags. The issue as Timotheos is that HTML has no idea you click the delete button that happens to be close to the hidden variable that you have. The button will submit the entire form. (ie *EVERY* form variable between <form> and </form>) but since you have many variables the same name (ie variableName) each new occurance overwrites the last. So no matter what delete button you push it submits the same form with the same variables and since the last "variableName" is the one that's set that's the one that gets deleted.

daisho.

Timotheos

7:39 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or around each "Delete" button and hidden variable add <form></form> tags.

There you go. I knew somebody would have the answer.

Personally I like tables that have the ability to delete more then one row at a time by simply clicking a check box in that row then clicking submit. A little bit more complicated to code but alot easier for the end user.

Imy_S3

10:01 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



This is my code, where do u suggest i put it?

Thanks in advance

for($j=0; $j<$nrows; $j++)
{
echo ( '<form action="deleteShoppingSelection.php" method="post">' );
$row = pg_fetch_array ($result);
printf ("<tr><td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>", $myrow['shopid'],$myrow['shopname'], $myrow['shopaddresslineone'], $myrow['shopaddresslinetwo'], $myrow['shopcounty'], $myrow['shoppostcode'], $myrow['shoptelephone'], $myrow['shopfax'], $myrow['shopemail'], $myrow['shopwebsite']);

print "<tr><td>" . $row["shopid"];
print "<td>" . $row["shopname"];
print "<td>" . $row["shopaddresslineone"];
print "<td>" . $row["shopaddresslinetwo"];
print "<td>" . $row["shopcounty"];
print "<td>" . $row["shoppostcode"];
print "<td>" . $row["shoptelephone"];
print "<td>" . $row["shopfax"];
print "<td>" . $row["shopemail"];
print "<td>" . $row["shopwebsite"];
print "<td><INPUT TYPE=text NAME=variableName VALUE=".$row["shopid"]." SIZE=5>";
print" <td><input type=submit value='Delete This Record'";
print" <td><input type=button value='Back' onClick='history.go(-1)'>";
//print "<td><a href="deleteShoppingSelection.php?shopid=".$row['shopid'].">Delete</a>";
print "<td><a href=\"deleteShoppingSelection.php?shopid=".$row['shopid']."\">Delete</a>";

print "\n";
}

Timotheos

10:53 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Daisho's suggestion would be like this...

for($j=0; $j<$nrows; $j++)
{
// NOTE: took out form tag here.
$row = pg_fetch_array ($result);
printf ("<tr><td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>", $myrow['shopid'],$myrow['shopname'], $myrow['shopaddresslineone'], $myrow['shopaddresslinetwo'], $myrow['shopcounty'], $myrow['shoppostcode'], $myrow['shoptelephone'], $myrow['shopfax'], $myrow['shopemail'], $myrow['shopwebsite']);

print "<tr><td>" . $row["shopid"];
print "<td>" . $row["shopname"];
print "<td>" . $row["shopaddresslineone"];
print "<td>" . $row["shopaddresslinetwo"];
print "<td>" . $row["shopcounty"];
print "<td>" . $row["shoppostcode"];
print "<td>" . $row["shoptelephone"];
print "<td>" . $row["shopfax"];
print "<td>" . $row["shopemail"];
print "<td>" . $row["shopwebsite"];
print "<td><form action=\"deleteShoppingSelection.php\" name= \"form" . $row['shopid'] . "\" method=\"post\">
<INPUT TYPE=\"hidden\" NAME=\"variableName\" VALUE=\"" . $row['shopid'] . "\">
<input type=\"submit\" value=\"Delete This Record\"></form>";
print" <td><input type=button value='Back' onClick='history.go(-1)'>";
print "\n";
}

Well I hope that works. It's untested. You should put in closed </td> tags where necessary.

Imy_S3

11:25 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



thanks very much mate, your a life saver.

Worked beautifully.

cheers

Timotheos

12:48 am on Mar 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I love a happy ending ;-)