Jump to content

delete desired db entries with checkboxes


xfezz

Recommended Posts

I would like to be able to have a list of all my entries in my mysql db with corresponding checkmark boxes. And then upon submission, delete the entries that were check marked. I have googled and searched the forums on here and came up with some useful info in another thread. BUT I cannot get it to work correctly.

I have the following to display the output with the checkboxes.

[code]
if ($db_found) {
    $SQL = "SELECT * FROM articles ORDER BY id DESC";
    $result = mysql_query($SQL);
      while ($db_field = mysql_fetch_assoc($result)) {
        echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$db_field[id]\" />"
        $now_format = $db_field['article_date'];
        $new_format = date('F d, Y',strtotime($now_format)); // transform
        echo $new_format ."<BR>";  // prints out: date in following format October 20, 2006
        echo $db_field['message'] . "<p>";
}
[/code]

Which does generate the checkbox with the correct id value to the corresponding entry of the database. Here is the source of the html to the output.

[code] <input type="checkbox" name="delete[]" value="5" />October 21, 2006<BR>this is article number 5<p>
[/code]

And so forth for the remaining entries in the db. My thinking is to use an array delete[5] where 5 is the id number to the entry waiting to be deleted. 

Now for the actual deleting of the entries, I have no idea on what to do here. Logically I think I know what I have to to. Have a loop that would go through and delete the entries with the corresponding id numbers. (the ones that were checked and passed to the $_POST[‘delete’] array.) or it should have been?

[code]
// if form has been submitted, run this:
if (count($_POST['delete']) > 0) {
  foreach ($_POST['delete'] as $val) {
    $sql = "DELETE FROM articles WHERE id = '$val'";
    mysql_query($sql);
  }
}
[/code]

(I got this from another thread that was posted here)

No workies. Im not even sure if the _$POST data values are being passed correctly or even html can do this. Or im just not pulling the data correctly from the array ;[
Link to comment
Share on other sites

Try this:

This is the part that deletes the messages after they select which ones to delete.
[code]

if ($_POST['delete']){
$delMessages = $_POST['delMsg'];
$num = count($delMessages);
for($i = 0; $i < $num; $i++){

$val = $delMessages[$i];

mysql_query("DELETE FROM articles WHERE id = '$val''");

}
}
[/code]

Use this for your while loop:

[code]

while ($db_field = mysql_fetch_assoc($result)) {
        echo "<input type=\"checkbox\" name=\"delMsg[]\" value=\"' . $db_field['id'] . '\">"
        $now_format = $db_field['article_date'];
        $new_format = date('F d, Y',strtotime($now_format)); // transform
        echo $new_format ."<BR>";  // prints out: date in following format October 20, 2006
        echo $db_field['message'] . "<p>";
}

[/code]

Also, use this delete button:

[code]
<input type="submit" name="delete" value="delete">
[/code]

Let me know how that goes =)
Link to comment
Share on other sites

hmm i get a

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Apache\htdocs\port\del3.php on line 27

line 27 is this line right here

[code]while ($db_field = mysql_fetch_assoc($result)) {[/code]

on the output after I tried what you said. doesnt even display the output. i think im going to have to redo my loop. or just redo everything  >:(
Link to comment
Share on other sites

Hmmm....sorry I couldn't be more help.

I just thought of something....did you change this:

[code]
echo "<input type=\"checkbox\" name=\"delMsg[]\" value=\"' . $row['messageID'] . '\">"
[/code]

to:

[code]
echo "<input type=\"checkbox\" name=\"delMsg[]\" value=\"' . $db_field['id'] . '\">"
[/code]



Link to comment
Share on other sites

Actually I forgot to change the field name to id instead of MessageID. but its not even get past line 27 so its not getting to it yet. I think im going to take a break. And maybe try again later. Thanks for the help though ;]

edit:

wow haha i got it to work. I feel like an idiot. I forgot to close the database connection and that got it to work. ima lock this for now.
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.