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
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
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

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