Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't delete from a MySQL Database Using PHP

Status
Not open for further replies.

egims

Technical User
Jun 5, 2001
96
0
0
US
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

<?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);
?>
 
looks ok save for some quote mistakes. let's add some debugging to make sure.

the error I believe comes solely from your quoting. because you are placing the $id inside the outer pair of single quotes, the variable will not expand. read up on variable expansion in the php manual.

normal practice is to enquote query strings in double quotes and query variables inside single quotes

Code:
<?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>";

if(isset($_POST['id'])):
mysql_connect(localhost,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$id = mysql_real_escape_string($_POST['id']);
$result = mysql_query("DELETE FROM inst_order WHERE id='$id'");
if ($result === false) echo mysql_error();
print $id;
endif;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top