?
Solved

Delete database entries using checkboxes

Posted on 2004-10-18
9
Medium Priority
?
286 Views
Last Modified: 2011-09-20
I am tring to make a gernic php script that I can use to delete entries in my sql db.

This is what have so far.

This first bit of code calls the inc file.  This html passes the name of the database that I want to delete from.
The second bit of code builds the table from the db and adds check boxes and then passes that on to another page that would delete each record (havn't built that part yet, but that is easy once I get this problem fixed).  

Here is my problem:
I want to detete the each record that is checked.  I want to delete it by the key ID (first column when built).  I don't know how to pass that info into an array and then pass that array to the next page.

I will also need to know how many were passed to the array to know how many times to loop through the delete part.  I was thinking about using javascript to count the number of boxes checked and pass the number as a hidden field.

Any help would be great!  I am tring to keep this a gernic as possible so that I can use this through out my site.


**************html**************************
<?php
include('./../remove_db.inc');
?>

<HTML>
<head>
<title>Delete a Card</title>
</head>
<body>
<CENTER>
<H2>Delete a Card</H2>
</CENTER>
<?php
display_db_query("Card_Numbers");
?>

**************remove_db.inc**************************
<?php
//This script will get the information out of the card type table and display it in a select menu
include('./database.inc');
//connect to the database
mysql_connect($HOSTNAME, $USER, $PASSWORD) or die("Unable to connect to the database");

//select the db
mysql_select_db("$DATABASE");

//----------------------------Display table----------------------
function display_db_query($tablename){

$query_string = "SELECT * FROM $tablename";

//perform db query
$results = mysql_query($query_string) or die("display_db_query:" . mysql_error());

//find the number of columns
$column_count = mysql_num_fields($results) or die("display_db_query:" . mysql_error());

//start of form
print("<FORM METHOD=POST ACTION=\"delete.php\">\n<BR>\n");


//Start of table
print("<TABLE BORDER=1>\n");
      print("\t<TR>\n");
            
            for ($column_num = 0; $column_num < $column_count; $column_num++){
            $field_name = mysql_field_name($results, $column_num);
            print("\t\t<TH>$field_name</TH>\n");
            }
      print("\t\t<TH>Delete</TH>\n");      
      print("\t</TR>\n");


//print body of table
$counter = 0;
while($row = mysql_fetch_row($results)){
      print("\t<TR ALIGN=LEFT VALIGN=TOP>\n");
                  for ($i = 0; $i < $column_count; $i++){
                  print("\t\t<TD>$row[$i]</TD>\n");
                  }
      //set name to ID of each record
      $ItemToDelete  = $row[0];
      
      print("\t\t<TD><INPUT TYPE=CHECKBOX NAME=\"$ItemToDelete\" VALUE=\"$ItemToDelete\"></TD>\n");
      print("\t</TR>\n");
      $counter++;
      }
      
print("</TABLE>\n");
print("<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"SUBMIT\">\n<BR>\n");
print("</FORM>\n");
}
?>
0
Comment
Question by:jomama247
9 Comments
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 80 total points
ID: 12341608
>I want to detete the each record that is checked.  I want to delete it by the key ID (first column when built).  I don't know how to pass that info into an array and then pass that array to the next page.
Name the checkbox deleteItems[] then you will get $_POST['deleteItems'] which is an array holding the ids that have been chekced.
>will also need to know how many were passed to the array to know how many times to loop through the delete part.
the numbers of items check can be get by count($_POST['deleteItems']) then.
0
 

Author Comment

by:jomama247
ID: 12347505
I can't use the brackets [] in html.  How do you get around this?
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 12357444
Sure you can.

<input type="checkbox" name="deleteItem[]" value="id1" id="someUniqueID1" /><label for="someUniqueID1">some label</label><br />
<input type="checkbox" name="deleteItem[]" value="id2" id="someUniqueID2" /><label for="someUniqueID2">some label</label><br />
<input type="checkbox" name="deleteItem[]" value="id3" id="someUniqueID3" /><label for="someUniqueID3">some label</label><br />
<input type="checkbox" name="deleteItem[]" value="id4" id="someUniqueID4" /><label for="someUniqueID4">some label</label><br />
<input type="checkbox" name="deleteItem[]" value="id5" id="someUniqueID5" /><label for="someUniqueID5">some label</label><br />

now in the PHP after you'll submit the form, you'll get $_POST['deleteItem'] as an array with the selected IDs...
for example, if you check none, you'll get !isset($_POST['deleteItem']).
if you'll check the 2nd and 4th, you'll get:
$_POST['deleteItem'] = array(
    'id2',
    'id4'
)

etc.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 

Author Comment

by:jomama247
ID: 12358946
Sorry about the array thing in html.  I got that to work.  I however, cannot get the info out of the array after a I pass it to the next page.

This is the code I have:
                $ItemsToDelete = $_POST['deleteItems'];
            $Size = 1 + count($ItemsToDelete);
            $nameOfFields = $_POST['nameOfFields'];
            print("Name of the field to delete from: $nameOfFields <BR>");
            print("Number of items to delete: $Size <BR>");
            for ( $i = 0; $i <= $Size; $i++){
                  print("ID to Delete: $ItemsToDelete[$i] <BR>");
            }

When I view it this is what I get:
              Name of the field to delete from: Card_Numbers_ID
              Number of items to delete: 1
              ID to Delete:
              ID to Delete:

Any ideas?
0
 

Expert Comment

by:roki
ID: 12359050
There is a typo your code:

$ItemsToDelete = $_POST['deleteItems'];

should be $ItemsToDelete = $_POST['deleteItem']; (note - there is no 's' after Item)

This is on the basis of using Nomaed's code example above.

So, you could write:

$ItemsToDelete = $_POST['deleteItem'];
$Size = count($ItemsToDelete);
$nameOfFields = $_POST['nameOfFields'];
print("Name of the field to delete from: $nameOfFields <BR>");
print("Number of items to delete: $Size <BR>");
for ( $i = 0; $i < $Size; $i++){
       print("ID to Delete: $ItemsToDelete[$i] <BR>");
}

Hopefully this should work.

Roki
0
 

Author Comment

by:jomama247
ID: 12359304
Thanks for your help Roki, but I have an 's' on deleteItems.

Here is the newest copy of the code:

<?php
include('./database.inc');
//connect to the database
mysql_connect($HOSTNAME, $USER, $PASSWORD) or die("Unable to connect to the database");

//select the db
mysql_select_db("$DATABASE");

function display_db_query($tablename){

$query_string = "SELECT * FROM $tablename";

//perform db query
$results = mysql_query($query_string) or die("display_db_query:" . mysql_error());

//find the number of columns
$column_count = mysql_num_fields($results) or die("display_db_query:" . mysql_error());

//start of form
print("<FORM METHOD=POST ACTION=\"../delete.php\">\n<BR>\n");

//Start of table
print("<TABLE BORDER=1>\n");
      print("\t<TR>\n");
            for ($column_num = 0; $column_num < $column_count; $column_num++){
            $field_name = mysql_field_name($results, $column_num);
            $nameOfFields[] = $field_name;
            print("\t\t<TH>$field_name</TH>\n");
            }
      print("\t\t<TH>Delete</TH>\n");      
      print("\t</TR>\n");
      
//print body of table
$counter = 0;
while($row = mysql_fetch_row($results)){
      print("\t<TR ALIGN=LEFT VALIGN=TOP>\n");
                  for ($i = 0; $i < $column_count; $i++){
                  print("\t\t<TD>$row[$i]</TD>\n");
                  }
      //set name to ID of each record
      $ItemToDelete  = $row[0];
      $deleteItems[] = $ItemToDelete;
            
      print("\t\t<TD><INPUT TYPE=CHECKBOX NAME=\"$deleteItems[$counter]\" VALUE=\"$deleteItems[$counter]\"></TD>\n");
      print("\t</TR>\n");
      $counter++;
      }
print("</TABLE>\n");
print("<INPUT TYPE=\"HIDDEN\" NAME=\"tablename\" VALUE=\"$tablename\">");
print("<INPUT TYPE=\"HIDDEN\" NAME=\"nameOfFields\" VALUE=\"$nameOfFields[0]\">");
print("<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"SUBMIT\">\n<BR>\n");
print("</FORM>\n");
}
?>
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 12359377
no no:

change:
print("\t\t<TD><INPUT TYPE=CHECKBOX NAME=\"$deleteItems[$counter]\" VALUE=\"$deleteItems[$counter]\"></TD>\n");

to:
print("\t\t<TD><INPUT TYPE=CHECKBOX NAME=\"deleteItems[]\" VALUE=\"$deleteItems[$counter]\"></TD>\n");
0
 
LVL 3

Accepted Solution

by:
Boris Aranovich earned 1320 total points
ID: 12359422
even to:
print("\t\t<TD><INPUT TYPE=CHECKBOX NAME=\"deleteItems[]\" VALUE=\"{$ItemToDelete}\"></TD>\n");
0
 

Author Comment

by:jomama247
ID: 12361634
Thanks for all your help Nomaed.  It works like a champ!

Joe
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question