Solved

Delete database entries using checkboxes

Posted on 2004-10-18
9
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 20 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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