MySQL "DELETE FROM" with PHP

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
robmcfeters
Forum Newbie
Posts: 1
Joined: Wed Dec 21, 2005 8:09 pm

MySQL "DELETE FROM" with PHP

Post 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]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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"); 
?>
MaNiAC
Forum Newbie
Posts: 20
Joined: Fri Dec 23, 2005 4:20 am

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Check what the value of ID is...
Post Reply