Results 1 to 13 of 13

Thread: Cannot delete a row with PHP & MySQL

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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

  2. #2
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    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.
    Don't Rate my posts.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Cannot delete a row with PHP & MySQL

    You can have arrays embedded inside double quotes as follows:
    Code:
    $string = "{$array['index']}";
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    Member n0vembr's Avatar
    Join Date
    Jun 2005
    Location
    pilipinas kong minumutya
    Posts
    60

    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

  5. #5

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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
    Tried it with the exact same results

    I have no clue what's going on

  6. #6
    Member n0vembr's Avatar
    Join Date
    Jun 2005
    Location
    pilipinas kong minumutya
    Posts
    60

    Re: Cannot delete a row with PHP & MySQL



    did u try manually executing the statement in a query analyzer tool?
    thanks..salamat..arigatou..gracias

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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
    You need to use urly brackets to enclose arrays:
    { }
    Code:
    $result = mysql_query("DELETE FROM {$data[0]} WHERE ID IN({$data[1]})");
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Cannot delete a row with PHP & MySQL

    Quote 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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9
    New Member
    Join Date
    Aug 2005
    Posts
    1

    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.

  10. #10

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Cannot delete a row with PHP & MySQL

    Quote 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.
    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.

  11. #11
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    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)
    Don't Rate my posts.

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  13. #13

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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
  •  



Click Here to Expand Forum to Full Width