Jump to content

Deleting MySQL table data with PHP using check boxes


tracy

Recommended Posts

Is there any way to create a display in a browser using PHP and a MySQL table that has an empty check box beside each row.  When a user checks the boxes in said display and then clicks DELETE, for example, those entire rows with the check boxes are deleted from the MySQL table?  Is there a tutorial anywhere?  Thanks.

This ad is crazy.
make sure your database table has a auto increment column, then select all those id(s) from the table and add those values to each checkbox value, then, loop the delete form data deleting all rows, that match those ids, but make sure they are allowed to delete those rows...

I'll give you a quick example in a few minutes...


This is just quick example, if you questions just...

This is the example db scheme..

the table example is based on POLLS (voted?)

[code]CREATE DATABASE junk;

CREATE TABLE stuff (
  id int(10) unsigned NOT NULL auto_increment,
  uid int(10) unsigned NOT NULL default '0',
  pid smallint(5) unsigned NOT NULL default '0',
  vote tinyint(1) unsigned NOT NULL default '0',
  PRIMARY KEY  (id),
  KEY uid (uid)
) ENGINE=MyISAM;


INSERT INTO stuff VALUES (1, 1, 1, 0);
INSERT INTO stuff VALUES (2, 1, 2, 0);
INSERT INTO stuff VALUES (3, 2, 1, 0);
INSERT INTO stuff VALUES (4, 2, 2, 1);
INSERT INTO stuff VALUES (5, 3, 1, 1);
INSERT INTO stuff VALUES (6, 3, 2, 1);
INSERT INTO stuff VALUES (7, 1, 3, 1);
INSERT INTO stuff VALUES (8, 2, 3, 1);
INSERT INTO stuff VALUES (9, 3, 3, 1);[/code]

The checkbox deletion script..


[code]<?php

// first connect to the database, then select it

// change (user, pass) to you login information

mysql_connect ( 'localhost', 'user', 'pass' ) or die ( 'Connection Error: ' . mysql_error () );

mysql_select_db ( 'junk' ) or die ( 'Select Error: ' . mysql_error () );

// I am just assigning the user id here
// but this might come from a SESSION
// type variable...

/*
* we use the $id, so we allow that user to
* only delete records belonging to them.
*/

$id = 2;

// now we create the POST control structure
// this tells the script to process this part
// of the code when POST is sent to the script.
// we use hidden form variable to test this...

if ( isset ( $_POST['delete'] ) )
{
// now we check if we have anything to do
// this is based on checkboxes that have
// been checked...

if ( isset ( $_POST['remove'] ) )
{
// we have at least one box checked
// so we need to do some processing

// first we CAST all the values to
// what we are expecting type (INT)

$remove = array_map ( 'intval', $_POST['remove'] );

// now we do the delete on the table (stuff)
// we don't have to use any database string
// type protection, because we CAST all the
// $remove array variables to (INT)

// the query restricts deleting rows based on the uid

mysql_query ( "DELETE FROM stuff WHERE uid = " . $id . " AND id IN ( " . implode ( ', ', $remove ) . ")" );
}
}

// now lets show the form
// move out of PHP
?>
<html>
<head>
<title> Untitled Document</title>
</head>
<body>
<?php

// back to PHP

// we need to find out if we need to
// show the form, because we only show
// it when there are rows to delete

// so lets do the query to find out if
// we need to show the delete form

// we select based on the uid

$test = mysql_query ( "SELECT id, pid FROM stuff WHERE uid = " . $id );

// test if we have any results

if ( mysql_num_rows ( $test ) > 0 )
{
// we have some results so lets show the form
// switch out of PHP
?>
<form action='' method='post'>
<input type='hidden' name='delete' value='1' />
<?php

// back to PHP

// now we loop the results returned in the query
// building the checkbox inputs. we use an array
// remove[] as the input name, so all the checked
// inputs will be loaded in a single array remove

while ( $item = mysql_fetch_assoc ( $test ) )
{
echo " Delete Poll ID (" . $item['pid'] . ") <input type='checkbox' name='remove[]' value='" . $item['id'] . "' />\r\n";
echo " <br />\r\n";
}

// switch out of PHP
?>
<input type='submit' value='Delete Items' />
</form>
<?php
// back to PHP
}
else
{
// no more rows to delete...

echo "there are no more rows for this user id: (" . $id . ")";
}
// switch out of PHP
?>
</body>
</html>[/code]


printf

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.