Page 1 of 1

Can't edit and delete in the same table

Posted: Fri Apr 01, 2011 12:57 pm
by Vinnar
Hey guys

So my question is related to the delete and edit functions in a table

When I am able to delete entries, I can't edit

When I am able to edit entries, I can't delete

Let me explain... Started with the table script first (excerpt):

Code: Select all

echo "<form method = \"post\" action=\"{$_SERVER['PHP_SELF']}\">
<table>
<tr>
<td width=\"55\" class=\"formLabelsS2\"><input type=\"submit\" name=\"delete_mail\" value=\"Delete\" id=\"delete_mail\"></td>
</tr>
</table>";

echo "<table class=\"sortable\" id=\"query_quick2\" width=\"100%\" >\r\n";
echo "\t<tr><th class=\"sorttable_nosort\" ></th><th class=\"sorttable_nosort\" ></th>
<th class=\"sorttable_alpha\" >Promoter Locus</th>\r\n";


if($result->num_rows){
while ($row = $result->fetch_array()){
$RowCount ++;
$row_color = ($RowCount % 2) ? $color1 : $color2;
echo "\t<tr id=\"{$row['id']}\" class=\"$row_color\" >


<!--<form method = \"post\" action=\"{$_SERVER['PHP_SELF']}\">-->



<td><input type =\"hidden\" name = \"id\" value=\"{$row['id']}\"/></td>
<td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"{$row['id']} \"></td>
<td>{$row['pro']}</td>
<td><input type=\"submit\" name=\"edit_mail\" value = \"Edit\"/></td>



<!--</form>-->



</tr>";

}
}

echo "</table>";
echo "</form>"; 

Script for deleting entries (excerpt):

Code: Select all

} elseif(isset($_SESSION['user_id']) AND isset($_POST['delete_mail'])){
//user is deleting existing queries
$connect=db_connect_2();

if($_POST['checkbox']){
{
foreach($_POST['checkbox'] as $check)
{
$delete = mysqli_query($connect, "DELETE FROM mailing_list WHERE id = '$check'");

}
$msgs[] = "Entry deletion was successful!";
$body = "account.php";
} 

When "edit_mail" button is pressed:

Code: Select all

} elseif(isset($_SESSION['user_id']) AND isset($_POST['edit_mail'])){
//user is editing existing queries
$body = "mailingList_edit.php";

} 
A editing form is displayed based on the value of $id:

Code: Select all

<?php
//retrieve user information
$conn=db_connect_2();
$id = mysqli_real_escape_string($conn, $_POST['id']);
$result = $conn->query("SELECT * FROM mailing_list WHERE id = '$id';");
$mail = $result->fetch_array();
?>

<div class="viewTitles" >Edit Your Queries:</div>
<form method = "post" action = "<? echo $_SERVER['PHP_SELF'] ?>">
<table>
<tr><td width="88" class="formLabelsS2" align="left">Promoter Locus:</td></tr>
<tr><td><input class = "basicTextField" type="text" name="pro_edit" value="<? echo $mail['pro']; ?>"></td></tr>

</table>
OK so what happens is:

I can delete entries with no problems by checking off the entries I want to delete then click delete button

But when I try to click "edit" button near a specific entry (individually), it DOES refer to a specific entry, but right after i SORT the table using sorttable.js, it would always refer to the LAST entry.

So I am suspecting there's problem with positioning of the <form></form> inside the while loop (see the table script, as I put lot of space in between these lines for emphasis)

Note that I put these <form> and </form> lines as comment so I could do what I mention above, otherwise I couldn't even delete any entries if I leave these 2 lines to be part of the while loop BUT !! I could edit specific entry even after I sort the table


I know this is long thread, so please let me know if you need more clarification

Thanks.

Re: Can't edit and delete in the same table

Posted: Sat Apr 02, 2011 2:26 am
by cpetercarter
Think about it. Your form has several lines in it. Each one is linked to a hidden field containing the id of the thing you want to edit. When you click on the "Edit" submit button, your browser reads through the form data. It finds a value of id in the first hidden field, but then it finds another value for id in the second hidden field, and so on. At each stage it overwrites the old value of id with the new one, so it always submits the id associated with the last row in your table, no matter which "Edit" button you click.

The solution is, as you suggest, to have a separate form for each row of data. But you can't nest a form inside another form. You need to close the "delete" form before you open the first "edit" form.