Jump to content


Photo

delete desired db entries with checkboxes


  • This topic is locked This topic is locked
4 replies to this topic

#1 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 22 October 2006 - 03:09 AM

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.

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>";
}

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.

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

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?

// 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);
  }
}

(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 ;[


#2 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 22 October 2006 - 04:07 AM

Try this:

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

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''");

	}
}

Use this for your while loop:


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>";
}


Also, use this delete button:

<input type="submit" name="delete" value="delete">

Let me know how that goes =)

#3 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 22 October 2006 - 04:22 AM

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

while ($db_field = mysql_fetch_assoc($result)) {

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

#4 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 22 October 2006 - 04:28 AM

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

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

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

to:

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





#5 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 22 October 2006 - 04:36 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users