Page 1 of 1

MySQL "DELETE FROM" with PHP

Posted: Wed Dec 21, 2005 8:13 pm
by robmcfeters
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Can someone tell me what's wrong with this code, because I don't know....I just know that it won't delete what I want it do:

Code: Select all

<?php
$id = $_POST['id'];
$connection=mysql_connect("localhost", "username", "password");
mysql_select_db('database',$connection) or die(mysql_error());
$result = "DELETE FROM news WHERE id='$id'";
mysql_query($result);
header("Location: newsdelete.php");
?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Wed Dec 21, 2005 10:39 pm
by Burrito
if an int type your ID is, necessary to have single quotes around your value it is not.

although, think that is the problem I do not:

try displaying a sql error if the query fails.

Code: Select all

<?php 
$id = $_POST['id']; 
$connection=mysql_connect("localhost", "username", "password")
    or die(mysql_error()); 
mysql_select_db('database')
    or die(mysql_error());  
$result = "DELETE FROM news WHERE id='$id'"; 
mysql_query($result)
    or die(mysql_error());  
header("Location: newsdelete.php"); 
?>

Posted: Fri Dec 23, 2005 4:40 am
by MaNiAC

Code: Select all

<?php
$id = $_POST['id'];
$result = "DELETE FROM news WHERE id='$id'"; 
echo $result;
?>
Now try to run exactly that query (copy -> paste) in phpMyAdmin or whatever you use.
If there's an error, sql will give you a good description (most of the time) so you can solve it.

Posted: Fri Dec 23, 2005 4:58 am
by Maugrim_The_Reaper
Check what the value of ID is...