|
-
Aug 3rd, 2005, 01:52 AM
#1
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
-
Aug 3rd, 2005, 02:17 AM
#2
PowerPoster
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.
-
Aug 3rd, 2005, 04:14 AM
#3
Re: Cannot delete a row with PHP & MySQL
You can have arrays embedded inside double quotes as follows:
Code:
$string = "{$array['index']}";
-
Aug 3rd, 2005, 10:01 PM
#4
Member
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
thanks..salamat..arigatou..gracias 
-
Aug 3rd, 2005, 11:09 PM
#5
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.
 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 
Tried it with the exact same results 
I have no clue what's going on
-
Aug 3rd, 2005, 11:47 PM
#6
Member
Re: Cannot delete a row with PHP & MySQL
did u try manually executing the statement in a query analyzer tool?
thanks..salamat..arigatou..gracias 
-
Aug 4th, 2005, 12:39 AM
#7
Re: Cannot delete a row with PHP & MySQL
 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 
You need to use urly brackets to enclose arrays: 
{ }
Code:
$result = mysql_query("DELETE FROM {$data[0]} WHERE ID IN({$data[1]})");
-
Aug 4th, 2005, 12:40 AM
#8
Re: Cannot delete a row with PHP & MySQL
 Originally Posted by n0vembr
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.
-
Aug 4th, 2005, 10:00 AM
#9
New Member
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.
-
Aug 5th, 2005, 01:05 AM
#10
Re: Cannot delete a row with PHP & MySQL
 Originally Posted by n0vembr
did u try manually executing the statement in a query analyzer tool?
Ran the same statement in PHP Admin and it worked fine.
 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.
 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
 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.
-
Aug 5th, 2005, 06:34 AM
#11
PowerPoster
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)
-
Aug 5th, 2005, 09:54 AM
#12
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.
-
Aug 5th, 2005, 03:24 PM
#13
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|