Cannot delete a row with PHP & MySQL
I cannot for the life of me get this to work with my web site.
Code:
Invalid query: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '=' at line 1
I get that error with the following code:
Code:
if ($_POST['delete'] == "news" || "articles"){ $data = array($_POST['delete'], $_POST['id']); $result = mysql_query("DELETE FROM $data[0] WHERE ID =$data[1]"); if (!$result) { die('Invalid query: ' . mysql_error());}
I've also tried the following
Code:
if ($_POST['delete'] == "news" || "articles"){ $data = array($_POST['delete'], $_POST['id']); $result = mysql_query("DELETE QUICK FROM $data[0] WHERE ID =$data[1]"); if (!$result) { die('Invalid query: ' . mysql_error());}
and
Code:
if ($_POST['delete'] == "news" || "articles"){ $data = array($_POST['delete'], $_POST['id']); $result = mysql_query("DELETE FROM $data[0] WHERE ID IN($data[1])"); if (!$result) { die('Invalid query: ' . mysql_error());}
I've also tried encapsulating my php variables and my column names with single quotes to no avail.
This code works without issue with my blogging software I made a long time ago... at least I thought it did but I don't even remember anymore. I'd have to double check
Re: Cannot delete a row with PHP & MySQL
DELETE FROM " . $data["something"] . "some more stuff"
Something like that? I think from memory you can have normal variables inside the string, but with arrays it must be concatenated. I dunno, I've never passed an array element to a query that I can remember, but give it a try. :)
Re: Cannot delete a row with PHP & MySQL
You can have arrays embedded inside double quotes as follows:
Code:
$string = "{$array['index']}";
Re: Cannot delete a row with PHP & MySQL
dont u think it should be :
Code:
$result = mysql_query("DELETE FROM" . $data[0] ." WHERE ID IN(" .$data[1] . ")");
Having it like this
Code:
$result = mysql_query("DELETE FROM $data[0] WHERE ID IN($data[1])");
would make the SQL query exactly "DELETE FROM $data[0] WHERE ID IN($data[1])", which obviously would give an error...i think :bigyello:
Re: Cannot delete a row with PHP & MySQL
You can have arrays in sql queries like I have just fine. I do it in other queries to update my tables.
Quote:
Originally Posted by n0vembr
dont u think it should be :
Code:
$result = mysql_query("DELETE FROM" . $data[0] ." WHERE ID IN(" .$data[1] . ")");
Having it like this
Code:
$result = mysql_query("DELETE FROM $data[0] WHERE ID IN($data[1])");
would make the SQL query exactly "DELETE FROM $data[0] WHERE ID IN($data[1])", which obviously would give an error...i think :bigyello:
Tried it with the exact same results :(
I have no clue what's going on
Re: Cannot delete a row with PHP & MySQL
:confused:
did u try manually executing the statement in a query analyzer tool?
Re: Cannot delete a row with PHP & MySQL
Quote:
Originally Posted by n0vembr
dont u think it should be :
Code:
$result = mysql_query("DELETE FROM" . $data[0] ." WHERE ID IN(" .$data[1] . ")");
Having it like this
Code:
$result = mysql_query("DELETE FROM $data[0] WHERE ID IN($data[1])");
would make the SQL query exactly "DELETE FROM $data[0] WHERE ID IN($data[1])", which obviously would give an error...i think :bigyello:
You need to use urly brackets to enclose arrays: :)
{ }
Code:
$result = mysql_query("DELETE FROM {$data[0]} WHERE ID IN({$data[1]})");
Re: Cannot delete a row with PHP & MySQL
Quote:
Originally Posted by n0vembr
:confused:
did u try manually executing the statement in a query analyzer tool?
Use echo() to output the query text an ensure it is what you expect. Then try n0vemberssuggestion of executing that exact text on the command line.
Re: Cannot delete a row with PHP & MySQL
I think you're forgetting there have to be quotes round the string in the query.
So I think it's like this:
Code:
$result = mysql_query("DELETE FROM '" . $data[0] ."' WHERE ID IN '" .$data[1] . "'");
I added the single quotes... (if ID is a number in your database, you can delete the single quotes there)
I hope it works, it's my first post here on the forum, and i just know the basics of PHP/MySQL, so don't shoot me if I'm wrong. :wave:
Re: Cannot delete a row with PHP & MySQL
Quote:
Originally Posted by n0vembr
:confused:
did u try manually executing the statement in a query analyzer tool?
Ran the same statement in PHP Admin and it worked fine.
Quote:
Originally Posted by visualAd
You need to use urly brackets to enclose arrays: :)
{ }
Code:
$result = mysql_query("DELETE FROM {$data[0]} WHERE ID IN({$data[1]})");
What? I have several statements which alter rows and insert rows with arrays and they work fine without brackets. Why would delete require them? I'll try it just to double check but I don't understand that.
Quote:
Originally Posted by visualAd
Use echo() to output the query text an ensure it is what you expect. Then try n0vemberssuggestion of executing that exact text on the command line.
Havn't had the chance of using echo or print. I'm going to give this a try later but it seems the issue is specifically with the sql query
Quote:
Originally Posted by Geert
I think you're forgetting there have to be quotes round the string in the query.
Shouldn't matter and I've tried that anyway.
Re: Cannot delete a row with PHP & MySQL
Strings should be quoted... but numbers don't always have to be? I dunno, I spent alot of time on this when I first started working with MySQL, finding that I did it without ' and so on, and then finding it would work with some queries and not others (I'm guessing it might presume string integer to be an integer, while a string...string will crap it self..I could be wrong though)
Re: Cannot delete a row with PHP & MySQL
Numbers must not be quoted. Strings must be quoted. In MySql its doesn't always error when you do quote numeric data, but sometimes it does. ;)
Re: Cannot delete a row with PHP & MySQL
Thanks for the tips. I'll try a few things tonight and let you know of my success or failure