Jump to content

PHP Blog: Deleting Posts?


christo16

Recommended Posts

Hello all I have setup a blog based on php and mysql, it seems to working well.  However when I go to delete a post it will only let me delete one post at a time.  I have it setup so that you click the check box of the post you want to delete and it adds it to $_GET["variable"] and then deletes it from the DB after it you hit submit.  Should I do as a $_POST or is there a better way to do it?
Thank you!
Link to comment
https://forums.phpfreaks.com/topic/30776-php-blog-deleting-posts/
Share on other sites

You would have to make the check boxes into an array so they can be set on the processing page.

Checkboxes should look something like this
[code]<input type=checkbox name=check[] value=$id>[/code]

Then on the page that deletes them:
[code]<?php
if(isset($_GET['submit'])){
$del_array= array();
foreach($_GET['check'] as $val){
$del_array[] = $val;
}
$ids = implode(", ", $del_array);
$sql = "DELETE FROM tablename WHERE id IN ($ids)";
}
?>[/code]

Ray
Thank you so much for the help guys, but I still seem to be having trouble with it working, it doesn't seem to delete any now.
Here is how my checkbox is setup
[code]<Input type=checkbox value=\"$row[0]\" name=\"id\">[/code]

And the delete code:
[code]<?
//Connects to database
require_once('../../../db_login.php');
if(isset($_GET['submit'])){
$del_array= array();
foreach($_GET['id'] as $val){
$del_array[] = $val;
}
$ids = implode(", ", $del_array);
mysql_query("DELETE FROM blog WHERE id IN ($ids)'")
or die(mysql_error());
}
?>[/code]

Thanks again!
change
[code]<Input type=checkbox value=\"$row[0]\" name=\"id\">[/code]
to this
[code]<Input type=checkbox value=\"$row[0]\" name=\"id[]\">[/code]

Make sure your form has a method of "GET" and you name your submit button
[code]<input type=submit name=submit value=submit>[/code]
Ray

Archived

This topic is now archived and is 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.