Question:
How to delete a row from a MySQL database using PHP?
1970-01-01 00:00:00 UTC
How to delete a row from a MySQL database using PHP?
Four answers:
ryan b
2009-11-22 01:55:50 UTC
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]}'")
2009-11-22 01:20:59 UTC
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.
TheRealSutano
2009-11-22 01:02:22 UTC
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());
2009-11-21 17:30:45 UTC
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.)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...