Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Delete database entries using checkboxes

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
jomama247
Asked:
jomama247
2 Solutions
 
hernst42Commented:
>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
 
jomama247Author Commented:
I can't use the brackets [] in html.  How do you get around this?
0
 
Boris AranovichCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jomama247Author Commented:
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
 
rokiCommented:
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
 
jomama247Author Commented:
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
 
Boris AranovichCommented:
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
 
Boris AranovichCommented:
even to:
print("\t\t<TD><INPUT TYPE=CHECKBOX NAME=\"deleteItems[]\" VALUE=\"{$ItemToDelete}\"></TD>\n");
0
 
jomama247Author Commented:
Thanks for all your help Nomaed.  It works like a champ!

Joe
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now