Small PHP/MySQL query error.. please help...

Discussion in 'PHP' started by cowguru2000, May 5, 2007.

  1. #1
    I'm trying to delete a record from my database... before I get into the specifics here's the code:

    
    <?
    
    // I have all the correct username and password stuff up here...
    
    mysql_connect($host,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $id = $_POST['id'];
    
    echo $id;
    
    $query = "DELETE FROM 'apptsubmiss' WHERE 'id' = $id;";
    
    mysql_query($query) or die("Query error");
    
    echo "Successfully deleted.";
    echo "<form action='displayappts.php'><input type='submit' name='submit' value='Back' /></form>";
    
    ?>
    PHP:
    I echoed the $id just to make sure it was there. It was what I wanted, however, there's a query error. Is it because I included a variable in the query or is it because the query itself is just wrong?
     
    cowguru2000, May 5, 2007 IP
  2. Neoto

    Neoto Active Member

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #2
    Try this:

    
    $query = "DELETE FROM apptsubmiss WHERE id = '$id'";
    
    PHP:
     
    Neoto, May 5, 2007 IP
  3. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Instead of using single quotes around the fields / tables, use back quotes.

    
    $query = "DELETE FROM `apptsubmiss` WHERE `id` = '$id'";
    
    PHP:
     
    CodyRo, May 5, 2007 IP
  4. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #4
    no quotes for field name is OK
    but you should give single quotes to the value!!

    
    <?php
    $query = "DELETE FROM apptsubmiss WHERE id = '$id';";
    ?>
    
    Code (markup):
     
    grandpa, May 6, 2007 IP
  5. calvinmicklefinger

    calvinmicklefinger Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Is the semi-colon inside the double quotes a requirement?
     
    calvinmicklefinger, May 6, 2007 IP
  6. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #6
    $query = "DELETE FROM 'apptsubmiss' WHERE 'id' = $id;";

    should read

    $query = "DELETE FROM apptsubmiss WHERE id='$id'";

    the semi colon inside the query string is not mandatory but it wouldn't hurt if you keep it.
     
    legend2, May 7, 2007 IP