If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Deleting Query Problem

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-10, 19:21
stacson stacson is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
Post Deleting Query Problem

Hi there,

I'm having problems deleting a row from my database. With the query I have I can delete everything in my table, but what I need is to only delete the row that has the checkbox ticked! My PHP code is below, can someone please have a look at it and let me know how to delete just one row??

<?php
$noupdates=0;
include("dbconnect.php");
$email=$_GET['pending'];
$query = "DELETE FROM members WHERE pending=" .$email;
$result = @ mysql_query($query);
if (!$result) {
$message="problem with members table";
echo "<a href='../approve.php'><h2>Back to Approve page</h2></a>";
die($message);
}
if (mysql_affected_rows()==0){
echo "Failed update: ".$email."<br />";
echo "<a href='../approve.php'><h2>Back to Approve page</h2></a>";
} else {
header("location:../approve.php");
}
?>

Cheers
Reply With Quote
  #2 (permalink)  
Old 04-09-10, 02:25
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 8,770
text / string / char columns when used in SQL statements shouldbe encapsulated in quotes
eg
Code:
$query = "DELETE FROM members WHERE pending='" .$email."'";
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 04-09-10, 02:30
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 8,770
*** Moved to PHP forums, as the problem is with the PHP code, rahter than a MySQL issue ***
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 04-09-10, 05:01
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
Originally Posted by stacson
and let me know how to delete just one row??
and the following should delete just one row:
Code:
$query = "DELETE FROM members WHERE pending='$email' limit 1";
__________________
Mike
Reply With Quote
  #5 (permalink)  
Old 04-09-10, 07:29
stacson stacson is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
Smile

Hey thanks for the help but the above didn't work, but with sleep and a fresh head I got it to work!!

$email=$_GET['pending'];
$query = "DELETE FROM members WHERE pending=1 AND ".$email;
Reply With Quote
  #6 (permalink)  
Old 04-09-10, 14:33
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
Originally Posted by stacson View Post
Hey thanks for the help but the above didn't work, but with sleep and a fresh head I got it to work!!

$email=$_GET['pending'];
$query = "DELETE FROM members WHERE pending=1 AND ".$email;
I guess we foolishly looked at what your code was originally doing and then tried to do what you asked!

Out of curiosity:
  • Why are you putting the form variable "pending" into a variable called email???
  • What does $email now contain to make the above delete statement valid?
  • Is there anything to stop a user from entering a pending value of "1=1 or 1=1" which would then delete all the data in your table?
  • Does this now limit the delete to just one record as you originally asked?
__________________
Mike
Reply With Quote
  #7 (permalink)  
Old 04-10-10, 09:31
stacson stacson is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
The code was copied over from "approve.php" as it is pretty much the same and the email variable was called $email on that page - I just haven't altered it.

When a user signs up the pending column is automatically assigned "1", so $email holds the number 1. When I approve a user the pending column changes to "0".

I'm really new to all of this and I don't quite understand what you mean "1=1 or 1=1"...all I know is because I didn't specify the number 1 it was deleting everything including approved ("0") members. So by changing my query to $query = "DELETE FROM members WHERE pending=1 AND ".$email; stops all users from being deleted. I will change the $email variable to $pending so it reads eaiser. Hope I have answered your questions.
Reply With Quote
  #8 (permalink)  
Old 04-10-10, 12:19
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
Originally Posted by stacson
Hope I have answered your questions.
Perfectly - thank you.
__________________
Mike
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On