Can't edit and delete in the same table

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

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\
";
echo "\	<tr><th class=\\"sorttable_nosort\\" ></th><th class=\\"sorttable_nosort\\" ></th>
<th class=\\"sorttable_alpha\\" >Promoter Locus</th>\\r\
";


if($result->num_rows){
while ($row = $result->fetch_array()){
$RowCount ++;
$row_color = ($RowCount % 2) ? $color1 : $color2;
echo "\	<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):

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


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


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

Only one row with the name “id” is going to be recognized when you submit the form, that’s why the last entry is always the one you get when they are all in the same form.

You should not have <form> inside another <form>. They are not designed to allow that.

I would recommend that you either split your page into 2 pages, 1 for edit and 1 for delete, or change the edit to come from a link rather than a form.

If you make each item a link that you can click on when you want to edit it, then you can still have your checkbox there for deleting and only use the <form> for your delete function and keep it all on one page:


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\
";
echo "\	<tr><th class=\\"sorttable_nosort\\" ></th><th class=\\"sorttable_nosort\\" ></th>
<th class=\\"sorttable_alpha\\" >Promoter Locus</th>\\r\
";


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


<td><input type =\\"hidden\\" name = \\"id\\" value=\\"{$row['id']}\\"/></td>
<td><input name=\\"checkbox[]\\" type=\\"checkbox\\" id=\\"checkbox[]\\" value=\\"{$row['id']} \\"></td>
<td><a href=\\"EDIT_PAGE?id={$row['id'}\\">{$row['pro']}</a></td>
<td></td>


</tr>";

}
}

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

And clean up the unnecessary code from there.