Jump to content

Trying to learn how to select multiple checkboxes to delete entries from a DB


Recommended Posts

I am looking for help trying to select multiple checkboxes to delete entries from a DB via a php web page.  I know the SQL syntax on *how* to delete a row of data within a SQL database, just not how to create a php array where I can do multiple delete commands at once.  For example, if I have a PostgreSQL database with the following rows:

 

database1=# SELECT * FROM table1;

 

cid  |  home_location  |  asking_price

----+---------------------+-----------------

1      123 Main Street    200000

2      345 First Ave        210000

3      456 Frontage Rd  199900

4      678 5th Street        205000

5      1001 Elm Street    225000

 

I would like to display the results of this SELECT ALL query onto a php web page with checkboxes next to each row, giving me the ability to check multiple rows and then delete them from the database.

 

The php code to display the selections looks something like this:

 

<?php

// Connecting to the database server

$dbconnect = pg_connect("host=aaa.bbb.ccc.ddd dbname=database1 user=user1 password=password1") or die("Could not connect: " . pg_last_error());

 

//Performing the SQL query

$removequery = sprintf("DELETE FROM table1 WHERE CID="?");

 

 

How would you suggest that i put all the desired queries together to be able to delete various ones at the same time?  I assume that I need to create some sort of array and then be able to delete them all at once, just not sure of how to go about coding that to work.

 

im gona make your life way easier.....

 

basic check box array usage

 

<input type="checkbox" name="delete[]" id="id#" value='id#' />

 

id# being your cid number

 

then you pass it using a form post

 

heres the magic in php

 

if (isset($_POST['delete']))
{
foreach ($_POST['delete'] as $c => $id)
{
	$d = "DELETE FROM `yourtablename` WHERE `cid` = '".$id."';";
}
}

 

and you can now delete many boxes at the same time !

blacknight: The HTML example you posted is correct, but the PHP/MySQL code is wasteful and not recommended. Use the method outlined in my post instead, as it requires only one query no matter how many rows you want to delete.

 

Not to mention that you should not use quotation marks around numerical values in a query, and that you're missing both input validation and output escaping in your example. At the very least typecast the IDs to int, as it'll prevent attacks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.