Jump to content

How to delete a specific entry


tommy168

Recommended Posts

Hey guys

 

I have trouble picking the specific id of an entry i want to delete

 

so please take a look at the codes first

 

Here's the function for making the form:

 

function am_query_display_quick($page,$order ='DATE DESC'){
global $email;
$conn = db_connect_2();
$color1 = "tableRowOdd";
$color2 = "tableRowEven";
$RowCount = 0;

$result = $conn->query("SELECT pro,pro_update,ana,ana_update,cell,cell_update,cellother,cellother_update,gen,gen_update,genother,genother_update,author,author_update,other,other_update,id
FROM mailing_list
WHERE email = '$email'
ORDER BY $order LIMIT $start_row, $max_per_page;");

echo "<form method = \"post\" action=\".{$_SERVER['PHP_SELF']}.\">";
echo "<table class=\"sortable\" id=\"query_quick2\" width=\"100%\" >\r\n";
echo "\t<tr><th></th><th>Promoter
Locus</th><th>Update?</th><th>Anatomical Area</th><th>Update?</th><th>Cell
Type</th><th>Update?</th><th>Other Cell Type</th><th>Update?</th><th>Genetic
Background</th><th>Update?</th><th>Other Gen.
Back.</th><th>Update?</th><th>Author</th><th>Update?</th><th>Other</th><th>Update?</th><th></th>\r\n";

if($result->num_rows){
while ($row = $result->fetch_array()){
$RowCount ++;
$row_color = ($RowCount % 2) ? $color1 : $color2;

echo "\t<tr class=\"$row_color\" ><td><input type=\"submit\" name=\"edit_mail\" value = \"Edit\"/></td>
<td>{$row['pro']}</td><td>{$row['pro_update']}</td><td>{$row['ana']}</td>
<td>{$row['ana_update']}</td><td>{$row['cell']}</td><td>{$row['cell_update']}</td><td>{$row['cellother']}</td>
<td>{$row['cellother_update']}</td><td>{$row['gen']}</td><td>{$row['gen_update']}</td><td>{$row['genother']}</td>
<td>{$row['genother_update']}</td><td>{$row['author']}</td><td>{$row['author_update']}</td><td>{$row['other']}</td><td>{$row['other_update']}</td>
<td><input type=\"submit\" name=\"delete_mail\" value =\"Delete\"/></td>
<td><input type =\"hidden\" name = \"id\" value=\"{$row['id']}\"/></td>
</tr>";


}
}

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

 

And this is where the delete command comes into place (excerpt):

 

 

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

$sql2 = "DELETE FROM mailing_list WHERE id='$id'";
$result = mysqli_query($conn, $sql2) or mysqli_error($conn);

$msgs[] = "Query deleted successfully.";
$body = "account.php";

} 

 

 

yes it can delete entries all right

 

but it doesn't delete a specific entry when i click the "delete" button near it

 

somehow, it ALWAYS deletes the last row of entry at the table

 

as you can see, i echoed out $id as it's referring to the LAST entry as well

 

So my question is, how to delete a particular entry when i click the corresponding "delete" button??

 

Thanks

Link to comment
Share on other sites

Your form is set up wrong. With it set up as it is, the only id that will be present in the $_POST array is the last one in the form. You either need to break each record retrieved from the database into its own form, with its own submit button, or add a checkbox to each record's display to indicate it should be deleted, and use one submit button to delete all selected records. Option 2 is probably the most user-friendly.

Link to comment
Share on other sites

  • 2 weeks later...

You either need to break each record retrieved from the database into its own form, with its own submit button

 

Could you please elaborate on this point?

 

Because I want to be able to edit individual entry, so i think it's a better idea to have edit buttons right next to the entries

 

by the way just a quick update, the codes have been fixed as follow:

 

$result = $conn->query("SELECT query, link, pro,pro_update,ana,ana_update,cell,cell_update,cellother,cellother_update,gen,gen_update,genother,genother_update,author,author_update,other,other_update,date,id
FROM mailing_list
WHERE email = '$email'
ORDER BY $order LIMIT $start_row, $max_per_page;");

echo "<table class=\"sortable\" id=\"query_quick2\" width=\"100%\" >\r\n";
echo "\t<tr><th class=\"sorttable_alpha\" >Updated Query</th><th width=\"10\" class=\"sorttable_alpha\" >Link</th>
<th class=\"sorttable_alpha\" >Promoter Locus</th><th class=\"sorttable_alpha\" >Update</th>
<th class=\"sorttable_alpha\" >Anatomical Area</th><th class=\"sorttable_alpha\" >Update</th>
<th class=\"sorttable_alpha\" >Cell Type</th><th class=\"sorttable_alpha\" >Update</th>
<th class=\"sorttable_alpha\" >Other Cell Type</th><th class=\"sorttable_alpha\" >Update</th>
<th class=\"sorttable_alpha\" >Genetic Background</th><th class=\"sorttable_alpha\" >Update</th>
<th class=\"sorttable_alpha\" >Other Gen. Back.</th><th class=\"sorttable_alpha\" >Update</th>
<th class=\"sorttable_alpha\" >Author</th><th class=\"sorttable_alpha\" >Update</th>
<th class=\"sorttable_alpha\" >Other</th><th class=\"sorttable_alpha\" >Update</th>
<th class=\"sorttable_mmdd\" >Date</th><th class=\"sorttable_nosort\" ></th><th class=\"sorttable_nosort\" ></th>\r\n";

if($result->num_rows){
while ($row = $result->fetch_array()){
$RowCount ++;
$row_color = ($RowCount % 2) ? $color1 : $color2;
echo "<form method = \"post\" action=\"{$_SERVER['PHP_SELF']}\">";
echo "\t<tr id=\"{$row['id']}\" class=\"$row_color\" >
<td>{$row['query']}</td><td>{$row['link']}</td>
<td>{$row['pro']}</td><td>{$row['pro_update']}</td><td>{$row['ana']}</td>
<td>{$row['ana_update']}</td><td>{$row['cell']}</td><td>{$row['cell_update']}</td><td>{$row['cellother']}</td>
<td>{$row['cellother_update']}</td><td>{$row['gen']}</td><td>{$row['gen_update']}</td><td>{$row['genother']}</td>
<td>{$row['genother_update']}</td><td>{$row['author']}</td><td>{$row['author_update']}</td><td>{$row['other']}</td><td>{$row['other_update']}</td>
<td>{$row['date']}</td>
<td><input type=\"submit\" name=\"edit_mail\" value = \"Edit\"/></td>
<td><input type =\"hidden\" name = \"id\" value=\"{$row['id']}\"/></td>
<td><input type=\"submit\" name=\"delete_mail\" value =\"Delete\"/></td>
</tr>";
echo "</form>";


}
}

echo "</table>"; 

 

 

now a specific entry could be deleted

 

but another problem arises

 

 

Right AFTER i sort the table by clicking a column heading, say, promoter locus, (using sortable.js)

 

and then when i try to delete a specific entry, problem arises.

 

Instead of deleting the entry i want, it always deletes the LAST entry.

 

Now as you can see, i select a specific value for $id, using post method from a form

 

But shouldn't the value of $id point to the entry i want to delete when i click the "delete" button near it? (i mean it works fine before i sort the table)

 

Any help would be greatly appreciated

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.