Having problem with UPDATE

Discussion in 'PHP' started by minhazikram, May 24, 2012.

  1. #1
    this is my php query script

    if(isset($_GET['likeId'])){
    $id = $_GET['likeId'];// suppose id
    $insertLike ="UPDATE studentwall SET like='hello' WHERE id={$id}";
    $insertLikeQuery=mysql_query($insertLike,$con);
    if(!$insertLikeQuery){
    die('error'.mysql_error());
    }


    }// end if(isset($_GET['likeId']))


    and this is my error : 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 'like='hello' WHERE id=225' at line 1

    please help
     
    Solved! View solution.
    minhazikram, May 24, 2012 IP
  2. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #2
    "UPDATE studentwall SET like='hello' WHERE id='".$id."'"
     
    HostPlanz, May 24, 2012 IP
  3. minhazikram

    minhazikram Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Still it is not working. The table "studentwall" contains many columns like a,b,c,d and these were created a long time ago. Now When i create a new column "like" in this table and then insert value in it from the browser, the error shows : 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 'like='hello' WHERE id=225' at line 1

    But if i try updating values in the other columns like "a" with the same query aforementioned, then it works fine. But it is not working for the "like" column
     
    minhazikram, May 25, 2012 IP
  4. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #4
    That's because the data type of the "like" column and the data type of the value you're trying to enter is not the same.
     
    HostPlanz, May 25, 2012 IP
  5. #5
    Like is keyword and you can not use as column name that why getting error
     
    michaelhomas83, May 25, 2012 IP
  6. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #6
    yea, you're right.
     
    HostPlanz, May 25, 2012 IP
  7. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #7
    You can escape reserved keywords by enclosing it with back tick character(`).

    Example would be:
    "UPDATE studentwall SET `like`='hello' WHERE id='".$id."'"
    Code (markup):
    Hope it helps :)
     
    akhileshbc, May 27, 2012 IP