Solved

Delete database entries using checkboxes

Posted on 2004-10-18
9
275 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 20 total points
Comment Utility
>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
Comment Utility
I can't use the brackets [] in html.  How do you get around this?
0
 
LVL 3

Expert Comment

by:Boris Aranovich
Comment Utility
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
 

Author Comment

by:jomama247
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Expert Comment

by:roki
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
even to:
print("\t\t<TD><INPUT TYPE=CHECKBOX NAME=\"deleteItems[]\" VALUE=\"{$ItemToDelete}\"></TD>\n");
0
 

Author Comment

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

Joe
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now