Page 1 of 1

Can't seem to delete a record using PHP & MySQL

Posted: Tue Sep 07, 2010 3:44 pm
by egims
Sorry to post such a simple thing to many, but having spend much of the day on this unsuccessfully I'm stumped.

In short, using a form that passes and ID to the script I simply wish it to delete the record ID sent.

You will see a line in the script "print $id;". I simply added that to see if $id would print to verify that it contained the id entered in the form, which it did and does. Why won't this script delete the entered id from the database!!!! Help

***** PLEASE USE THE PHP CODE TAG *****

Code: Select all

<?PHP
$username="username";
$password="password";
$database="database";

print "<form method='POST' action='deletetest.php'>";
print "<pre>";
print "Enter Id Number to Edit: <input type='text' id='id' name='id' size='5'>";
print "<input type='submit' value='Submit'>";
print "</pre>";
print "</form>";

$id = $_POST['id'];
$con = mysql_connect(localhost,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("$database", $con);

mysql_query('DELETE FROM inst_order WHERE id='$id');
print $id;

mysql_close($con);
?>

Re: Can't seem to delete a record using PHP & MySQL

Posted: Tue Sep 07, 2010 6:31 pm
by Christopher
You have a syntax error because of quotes. Read the manual about the difference between " and '.

It should be:

Code: Select all

mysql_query("DELETE FROM inst_order WHERE id='$id'");
[/quote]

Re: Can't seem to delete a record using PHP & MySQL

Posted: Tue Sep 07, 2010 7:05 pm
by califdon
Is your id field a text field or a number? Most id fields are integers. If it is a number, you must NOT surround it with quotation marks. So your line would look like:

Code: Select all

mysql_query("DELETE FROM inst_order WHERE id=$id");