How to delete a row from a MySQL database using PHP?
-
I have a website and I need to delete a row from my database. Everything seems to be ok and when I run the script it shows no errors. Here is the script: mysql_query("DELETE FROM database WHERE id = '$_POST[id]'") or die(mysql_error()); . What is wrong with that script? I am the admin, and I have the privilege to use the DELETE MySQL command.
-
Answer:
TheRealSutano has his quotes a little mixed. Try 'DELETE FROM tablename WHERE id = \''.$_POST[id].'\';' for your SQL string (where tablename is the real table name, of course). Double quotes take more processing time. Two debugging hints: 1) Use Firefox, Firebug and FirePHP. Log your strings if there's a question about them, and you'll see, in Firefox, what the PHP code is creating. 2) Run a tail on the server error log (Baretail if you're running the server on a Windows computer). The error log will tell you where the error is, and what it is, if the server throws an error. Keeping a tail on the log (a window that keeps refreshing and shows the last lines) makes debugging server errors easy. (If you've never ever made a type you may not need this. Mere mortals do.)
Colanth at Yahoo! Answers Visit the source
Other answers
First, I assume you are already connected to your database. You aren't deleting from your database, you're deleting from a table You also cannot include a variable inside of your query. It has to be added separately. Also, it is better if you use the following syntax: $sql = "DELETE FROM table WHERE id =' . $_POST[id]; mysql_query($sql) or die(mysql_error());
TheRealSutano
That script is written fine, this is a semantic error. First I would check and see what's selected. I'm not sure how much you know about MySQL and PHP, so I'm not sure what method to suggest to you. Personally, I would test the conditions(the id) to see that the ID given is right. To do this you could change the DELETE FROM to a SELECT * FROM and then display the results in a table. If the ID given matches up to the ID that you would like to delete then make sure you're deleting from the correct table... And yeah, as the guy above said, you don't delete from the database, you delete from the table. But your table might be named "database" so I'm not sure how useful that statement is.
Dan S
The proper way to format this: mysql_query("DELETE FROM database WHERE id = '$_POST[id]'") having the $_POST inside the string is as follows: mysql_query("DELETE FROM database WHERE id = '{$_POST[id]}'")
ryan b
Related Q & A:
- How to create a Restful web service in .Net Using MySQL?Best solution by stackoverflow.com
- How to highlight a row in a listview?Best solution by Stack Overflow
- How to delete a line from a .txt file in node.js?Best solution by c-sharpcorner.com
- HOW TO DELETE A MEMBER IN A GROUP?Best solution by Yahoo! Answers
- How to delete a contact from a favorite list?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.