MySQL Error

Discussion in 'PHP' started by Pudge1, Dec 20, 2009.

  1. #1
    
    $sql="INSERT INTO Users (Username, Password, EMail)
    VALUES
    (" . $username . "','" . $password . "','" . $email . ")";
    
    Code (markup):
    What am I doing wrong with the syntax of this. I just can't figure it out.
     
    Pudge1, Dec 20, 2009 IP
  2. NodLemon

    NodLemon Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $sql = "INSERT INTO Users (Username, Password, EMail) VALUES ('$username', '$password', '$email')";
    PHP:
     
    NodLemon, Dec 20, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You're missing the single quote after the opening bracket
     
    JAY6390, Dec 20, 2009 IP
  4. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    Thank you!
     
    Pudge1, Dec 20, 2009 IP
  5. jedi.knight

    jedi.knight Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hope you are escaping possible special characters in the variables before putting them into $sql. E.g.:

    
    $username = mysql_real_escape_string($username);
    
    PHP:
     
    jedi.knight, Dec 20, 2009 IP
  6. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #6
    Of course I am.
     
    Pudge1, Dec 21, 2009 IP
  7. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $sql = "INSERT INTO `Users` (`Username`, `Password`, `EMail`) VALUES ('{$username}', '{$password}', '{$email}');";
    PHP:
    That's how I'd personally write it.
     
    CodedCaffeine, Dec 23, 2009 IP
  8. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #8
    Vote up for this way.
     
    Gray Fox, Dec 23, 2009 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I always use the following
    $sql = mressf("INSERT INTO `Users` (`Username`, `Password`, `EMail`) VALUES ('%s' '%s', '%s');", $username, $password, $email);
    PHP:
    This will insert the values after running them all through mysql_real_escape_string. The mressf function is one I created to stop having to escape each value individually. It works in the exact same way sprintf does with the added bonus of escaping all the values.

    Find the function here
     
    JAY6390, Dec 23, 2009 IP
  10. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #10
    Thank you everyone. But this problem has already been solved.
     
    Pudge1, Dec 23, 2009 IP