Solved

delete multiple records

Posted on 2009-03-30
4
509 Views
Last Modified: 2013-12-12
I need to delete multiple records for a database table using a specific id that I receive via URL parameter from the previous page. I found a code similar to this however I am not sure if this would loop to the records as for each specific id I need to delete several records.

I also don't understand "if($_GET['justdel'])". Why do I have to have this again?
<?php

/* delete.php */

$host = "localhost";

$username = "root";

$password = "";

$database = "delsampel";

$connection = mysql_connect($host, $username, $password);

mysql_select_db($database, $connection) or die("MysQL Error");

$id = $_GET[del];

$count = count($id); //counting how many rows to delete

if($_GET['justdel'])

{

for ($i=0; $i<$count; $i++)

{

$sql = mysql_query("delete from tabledelete where id=$id[$i]");

}

if ($sql)

{

print "Record successfully deleted<br>";

print "<a href=view.php>Click here to go back</a>";

}

}

?>

Open in new window

0
Comment
Question by:michael789
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
HMoellendorf earned 300 total points
ID: 24024816
Hi michael,

thanks for posting at EE. I've seen your question and w'd like to assist you as best as I can.
I understand, that you have an ID in your GET-Variables given from another page, is that correct?

If it is so, you can only use something like my snippet.
It is very important that you will check your id through some functions, i.e. is_integer() or others like that.

The example you gave us supports more than one id in one variable.
You should decide, what you need.

Kind regards,
Henning
<?php

if(isset($_GET["delid"])) {

 //do some verification here

 mysql_query("DELETE FROM table WHERE id = '".$_GET["delid"]."'");

}

?>

Open in new window

0
 
LVL 3

Assisted Solution

by:FirestormX
FirestormX earned 200 total points
ID: 24042097
I don't think that the code posted in the question will work as expected, as line number 9 and 10 of the code won't work; count() counts the number of indexes in an array, and you cannot pass an array through a GET variable.

I'm not sure what knid of data is being sent in the URL data (we'll call it $_GET['id']), but I would think that it would have to be multiple IDs, seperated by commas or spaces.
Assuming commas are used, $_GET['id'] would look like this:
$_GET['id'] = "4,8,15,16,23,42";

You can then use a code like this:
<?php

if (isset($_GET['id']))

{

	$ids = explode(",", $_GET['id']); //Put each ID in an array

	foreach ($ids as $id) //Loop through the array

	{

		if (is_numeric($id))

		{

			mysql_query("DELETE FROM table WHERE id='".mysql_real_escape_string($id)."' LIMIT 1");

		}

	}

}

?>

Open in new window

0
 
LVL 3

Assisted Solution

by:HMoellendorf
HMoellendorf earned 300 total points
ID: 24046977
I'm sorry to correct you, FirestormX, but you can pass a array as an GET-Variable.
http://domain.tld/index.php?id[]=1&id[]=2&id[]=3

You will get an array of $_GET["id"];

Kind regards,
Henning
0
 
LVL 3

Assisted Solution

by:FirestormX
FirestormX earned 200 total points
ID: 24048879
Oh, I didn't know that!
I'm sorry to have said that; please disregard that part of my message Michael. (Thanks Henning)

If that's the case then, I assume you could do something like this (I don't know how your form is setup, but I'll create a sample form of how I would do it. Also, keep in mind that there is a limit to how long a URL can be, so using GET variables is limited):
<?php

//Create the form (this will pull all the records in the table, and display them with a checkbox to delete them
 

print '<form method="get" action="delete.php">'

$query = mysql_query("SELECT id, title FROM table") or die(mysql_error());

while ($row = mysql_fetch_array($query))

{

print '<input type="checkbox" name="id[]" value="'.$row['id'].'">'.$row['title'].'</input><br />';

}

print '<input type="submit">';

print '</form>';
 
 

//Now the PHP in delete.php

if (isset($_GET['id']))

{

foreach ($_GET['id'] as $delete_id)

{

if (is_numeric($delete_id))

{

mysql_query("DELETE FROM table WHERE id='".mysql_real_escape_string($delete_id)."' LIMIT 1") or die(mysql_error());

}

}

}

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 57
WordPress Themes 10 34
Print a zero when there are no results in a PHP MySQL COUNT query 15 29
Scope of $_SESSION 17 30
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

895 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

13 Experts available now in Live!

Get 1:1 Help Now