User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,697 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,148 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 4440 | Replies: 22
Reply
Join Date: May 2005
Posts: 228
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

mysql DELETE not working

  #1  
Oct 18th, 2006
Hey everyone, I'm using php to delete a row from a table in a database in mysql, but it's not working. I'm not even trying to do anything fancy, just delete one row. Here's the code:

[PHP]<?
include 'config.php';

$recordid = $_POST['recordid'];

echo $recordid;

mysql_query("DELETE FROM employees WHERE recordid='$recordid'")
or die('sorry, no query');


echo "You have successfully deleted the user."


?>[/PHP]

The database is connecting properly, I double checked that. The variable $recordid is echoing out properly, and I even tried not using the variable, and just placing the actual number of the record id in the code instead, but still didn't work. And yes, I did try copying and pasting that code directly in to the mysql command line. Works perfectly no errors. So how come the query never works? The query dies every time, no fail, but works every time when I use the exact same command in the mysql command line, straight copy and paste, no kidding.

Any ideas? I know it's going to be something stupid, and I'm going to kick myself when I find out, but it's killing me!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2006
Location: NYC
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

Re: mysql DELETE not working

  #2  
Oct 18th, 2006
change your query to this:
[php]
mysql_query("DELETE FROM employees WHERE recordid='".$recordid."'")
[/php] you cant wrap a variable in single quotes as it will be trated as literal.
Last edited by sn4rf3r : Oct 18th, 2006 at 11:26 am. Reason: spelling
Reply With Quote  
Join Date: Jul 2004
Location: North East Indiana
Posts: 491
Reputation: Puckdropper is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 20
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: mysql DELETE not working

  #3  
Oct 18th, 2006
Check for errors with mysql_errorno or a similar function. That might help you out some.
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote  
Join Date: May 2005
Posts: 228
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: mysql DELETE not working

  #4  
Oct 19th, 2006
Originally Posted by sn4rf3r View Post
change your query to this:
[php]
mysql_query("DELETE FROM employees WHERE recordid='".$recordid."'")
[/php] you cant wrap a variable in single quotes as it will be trated as literal.

Thanks, but that doesn't seem to work either. Plus, I'm pretty sure I've done mysql_query's before with variables in single quotes and it works just fine. And besides, I'v even tried just putting the actual record id in that space, and it doesn't work. For instance, I put:
[PHP]mysql_query("DELETE FROM employees WHERE recordid='8'")[/PHP]
And it still doesn't work. I don't understand!
Reply With Quote  
Join Date: May 2005
Posts: 228
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: mysql DELETE not working

  #5  
Oct 19th, 2006
Originally Posted by Puckdropper View Post
Check for errors with mysql_errorno or a similar function. That might help you out some.


How exactly do you use mysql_errorno? I can't find any documentation on it. I even looked on http://dev.mysql.com and couldn't find it. I did try mysql_error, and it spit this error out:
Access denied for user: 'user@10.0.0.1' to database 'mydatabase'

I changed the username, IP, and database name, just in case you're wondering. The problem is, I'm not having problems connecting to the database. the config.php file contains a mysql_error that should tell me if there were any connection problems. And the user that this error is telling me can't get access is the same user getting access in all my other scripts. And they're not having any problems. This is really buggin.
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 1,289
Reputation: vishesh is on a distinguished road 
Rep Power: 4
Solved Threads: 32
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: mysql DELETE not working

  #6  
Oct 19th, 2006
so be carefull while using keywords. it's dangerous.
Reply With Quote  
Join Date: May 2005
Posts: 228
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: mysql DELETE not working

  #7  
Oct 19th, 2006
Originally Posted by vishesh View Post
so be carefull while using keywords. it's dangerous.

What?
Reply With Quote  
Join Date: Mar 2006
Location: Florence Ky
Posts: 31
Reputation: DGStudios is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 3
DGStudios DGStudios is offline Offline
Light Poster

Re: mysql DELETE not working

  #8  
Oct 19th, 2006
One thing you might try is to store you're query in a variable before putting it into the mysql_query() function.

$query = "DELETE FROM employees WHERE recordid='$recordid'";
$results = @mysql_query($query);


Mind You I used the storing of results. This is good practice as it allows for good error checking. I use it all the time, as queries ALWAYS return a value. while Selects and whatnot actually return values the others return a true/false value set.... or at least false when they fail.

Try it that way and see if that fixes it. If not.. take the query into a database interface and run the query. You might have to echo the query out to get the exact query, and just run it. That will give you a better understanding of exactly WHERE the issue is in the query.
http://img.photobucket.com/albums/v6.../dgstudios.jpg
When all that is becomes one. That is the anomoly. That is... Death's Gate Studios (c) 2005
Reply With Quote  
Join Date: Sep 2006
Location: NYC
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

Re: mysql DELETE not working

  #9  
Oct 19th, 2006
just looks like he cant connect to the database from where he is.

mysql> select * from mysql.user where user = 'username'
\G;
mysql> GRANT ALL PERMISSIONS ON *.* TO 'joe'@'10.0.1.1' IDENTIFIED BY 'password'
Reply With Quote  
Join Date: May 2005
Posts: 228
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: mysql DELETE not working

  #10  
Oct 20th, 2006
Originally Posted by DGStudios View Post
One thing you might try is to store you're query in a variable before putting it into the mysql_query() function.

$query = "DELETE FROM employees WHERE recordid='$recordid'";
$results = @mysql_query($query);


Mind You I used the storing of results. This is good practice as it allows for good error checking. I use it all the time, as queries ALWAYS return a value. while Selects and whatnot actually return values the others return a true/false value set.... or at least false when they fail.

Try it that way and see if that fixes it. If not.. take the query into a database interface and run the query. You might have to echo the query out to get the exact query, and just run it. That will give you a better understanding of exactly WHERE the issue is in the query.


Thanks, I'll give that a try.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 2:40 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC