Author Topic: Looking to Delete Table Entries?  (Read 617 times)

PHP_Person

  • Regular Member
  • **
  • Posts: 56
  • Karma: 2
    • View Profile
Looking to Delete Table Entries?
« on: November 18, 2011, 03:16:25 PM »
This is a working script that lists the table entries then deletes the ones selected.

The script:
PHP Code: [Select]

<?php
mysql_connect
("localhost""forumdb""asd123");
mysql_select_db("forumdb");
$checked $_POST['checked'];
foreach (
$checked as $value) {
  
mysql_query("DELETE FROM `forumdb`.`topictbl` WHERE `topictbl`.`topicid` = $value") or die(mysql_error());
}
?>

<form action="index.php" method="POST">
<?php
  $result 
mysql_query("SELECT * FROM topictbl");
  while(
$row mysql_fetch_array($result)) {
    echo 
"<input type='checkbox' name='checked[]' value='" $row['topicid'] . "' />" $row['topicname'] . "<br />";
  }
?>

<input type="submit" value="delete" />
</form>


The explanation . . .

First, we'll have this:

Code: [Select]
<?php
mysql_connect
("localhost""user""pass");
mysql_select_db("topicdb");

Simple enough, right? We're just connecting to mysql.  Next, we want to find every checkbox selected, so we'll add to that code:

Code: [Select]
$checked = $_POST['checked'];
foreach($checked as $value) {
  mysql_query("DELETE FROM `forumdb`.`topictbl` WHERE `topictbl`.`topicid` = $value") or die(mysql_error());
}
?>

$checked is all the variables we got from the form.  Then it does foreach loop, which means for each checked checkbox, do this.  Then it runs a simple mysql query that delets the entry by id.  To write the form, then, we have this:

Code: [Select]
<form action="index.php" method="POST">
<?php
  $result 
mysql_query("SELECT * FROM topictbl");
  while(
$row mysql_fetch_array($result)) {
    echo 
"<input type='checkbox' name='checked[]' value='" $row['topicid'] . "' />" $row['topicname'] . "<br />";
  }
?>


<input type="submit" value="delete" />
</form>

Pretty self-explanatory; while Selecting * from topictbl, write the checkbox. ;)
< Click here to maximize the image.

dyr

  • Regular Member
  • **
  • Posts: 42
  • Karma: 2
    • View Profile
    • Virtus
Re: Looking to Delete Table Entries?
« Reply #1 on: March 27, 2012, 05:33:15 PM »
Thanks for this!  Are there any security measures you recommend for using this feature front-end like you have?  So far all I've done is declared that admins are the only ones who are allowed access to this.

daveismyname

  • Senior Member
  • ****
  • Posts: 301
  • Karma: 6
  • PHP Helper
    • View Profile
    • PHP Help Tutorials
Re: Looking to Delete Table Entries?
« Reply #2 on: March 27, 2012, 05:38:33 PM »
I'd secure the post data to be on the safe side

PHP Code: [Select]
$checked mysql_real_escape_string($_POST['checked']);

dyr

  • Regular Member
  • **
  • Posts: 42
  • Karma: 2
    • View Profile
    • Virtus
Re: Looking to Delete Table Entries?
« Reply #3 on: March 28, 2012, 08:08:58 PM »
I seem to be getting the error: Invalid argument supplied for foreach()

Is that because you did not cast the foreach in to an array?

PHP_Person

  • Regular Member
  • **
  • Posts: 56
  • Karma: 2
    • View Profile
Re: Looking to Delete Table Entries?
« Reply #4 on: May 27, 2012, 08:42:02 PM »
I think the error comes from no records in the database.
< Click here to maximize the image.