Jump to content


Photo

DELETE query not working


  • Please log in to reply
4 replies to this topic

#1 dalty52

dalty52
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 July 2006 - 01:01 AM

Anyone have any ideas why the DELETE query at the bottom of this script is not working?  I just want to delete one row in the database that corresponds to the $id variable.  Thanks!

<?php 
require ('../mysqlconnect.php');
$id = $_GET['id'];
function form() {
	global $id;
	$q = "SELECT * FROM gigs WHERE id=$id";
	$result = @mysql_query ($q);
	if ($result) {
		while ($part = mysql_fetch_array
		($result, MYSQL_ASSOC)) {	
		print '<h1>Do you want to delete the gig: ' . $part['location'] . '?</h1>';
		print '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
		print '<input type="radio" name="delete" value="yes"/>Yes';
		print '<input type="hidden" name="check" value="1">';
		print '<input type="submit" name="submit" value="Submit">';
		}
}else {
	print "could not retrieve data";
	}
	}
if ($_POST['check']) {
	process();
	}
else {
	form();
	}
	
function process() {
		global $id;
		if ($_POST['delete']) {
		$id = $_GET['id'];
		$q = "DELETE FROM gigs WHERE id=$id";
		$result = @mysql_query($q);
			if ($result) {
				print 'Deleted!<br><a href="addgig.php">Return</a>';
			} else {
				print 'Could not connect to the database.';
				$e = mysql_error();
				print "$e";
			}
		}else { print 'Not Deleted';}
	}
?>


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 18 July 2006 - 01:26 AM

No, but you could find out by changing this:
$result = @mysql_query($q);

to this:
$result = mysql_query($q) or die("Error ". mysql_error(). " with query ". $query);

Legend has it that reading the manual never killed anyone.
My site

#3 dalty52

dalty52
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 July 2006 - 01:38 AM

The error returned this:  You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

The version on the server is 3.23.49, and I checked the manual but could not find any answers.  I'm sure it's something simple I am overlooking.  Thanks for the help!

#4 dalty52

dalty52
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 July 2006 - 02:43 AM

Ok, figured it out.  I was sending the form, so the $id variable lost it's value.  I created a hidden form input that spit out the id value and then posted it into the function I was having problems with.  Worked like a charm.  Thanks for the help.

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 18 July 2006 - 08:02 AM

It doesn't look like you're validating that UID at all... does that mean with one line of JS I could delete a random record in this table?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users