syntax error, unexpected T_STRING on INSERT command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jordan79
    New Member
    • Mar 2008
    • 20

    #16
    Originally posted by ronverdonk
    User_id is (most probably) a character type field, so you must enclose any value to be used with that field, between quotation marks, like[[php]$sql = "SELECT user_id FROM user WHERE user_id = '".$_SESSION['MM_username']."'";[/php]If it is a char field, then the same goes for line 12 statement.

    Ronald
    Yeah that was it.

    [PHP]$v=UPLOAD_DIR.$ _SESSION['MM_Username'].'/'.$now.$_SESSIO N['MM_Username'].'-'.$file;

    //copy image to database

    $sql ="SELECT user_id FROM user WHERE username = '".$_SESSION['MM_Username']."'";
    $queryresult = mysql_query($sq l)
    or die (mysql_error()) ;
    if (mysql_num_rows ($queryresult) > 0) {
    $row=mysql_fetc h_assoc($queryr esult);
    $userid=$row['user_id'];
    }

    $resins=mysql_q uery("INSERT INTO gallery (user_id, image_name) VALUES ($userid,'$v'")
    or die("Error in INSERT: ".mysql_error() );[/PHP]

    When I upload a file and it tries to send it to the database I get this error message:

    "Error in INSERT: 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"

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #17
      Please read my posts entirely
      If it is a char field, then the same goes for line 12 statement.
      So line 13 (was line 12 previously) must have the user_id within quotes just like the other user_id fields:[php]VALUES ('$userid','$v' ")[/php]Ronald

      Comment

      • Jordan79
        New Member
        • Mar 2008
        • 20

        #18
        Originally posted by ronverdonk
        Please read my posts entirely
        So line 13 (was line 12 previously) must have the user_id within quotes just like the other user_id fields:[php]VALUES ('$userid','$v' ")[/php]Ronald
        Sorry.
        I have been using this line:
        [PHP]$sql ="SELECT user_id FROM user WHERE username = '".$_SESSION['MM_Username']."'";[/PHP]

        where I get user_id from the user DB WHERE username is equal to the the session username.
        I understood that I was using this query to return user_id for the username logged in?

        Sorry if I picked u up wrong.
        That is why I didnt put the quotes around line 12 because user_id is an INT.

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #19
          Statement should then be[code=mysql]$sql ="SELECT user_id FROM user WHERE user_id = ".$_SESSION['MM_Username'];[/code]Ronald

          Comment

          • Jordan79
            New Member
            • Mar 2008
            • 20

            #20
            I have been working on my code.
            New Code

            [PHP]$v=UPLOAD_DIR.$ _SESSION['MM_Username'].'/'.$now.$_SESSIO N['MM_Username'].'-'.$file;

            //copy image to database

            $sql = "SELECT `user_id` FROM `user` WHERE `username` = '".$_SESSION['MM_Username']."'";
            $queryresult = mysql_query($sq l)
            or die (mysql_error()) ;
            if (mysql_num_rows ($queryresult) > 0) {
            $row=mysql_fetc h_assoc($queryr esult);
            $userid=$row['user_id'];
            }

            $resins=mysql_q uery("INSERT INTO gallery ('user_id', 'image_name') VALUES ($userid,'$v')" )
            or die("Error in INSERT: ".mysql_error() );[/PHP]

            I get this error
            "Error in INSERT: 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 ''user_id', 'image_name') VALUES (5,'C:\htdocs\P hotoABC\upload_ test\Jordan/2008-0' at line 1"

            As u can see the VALUES entered are correct.
            Just have an SQL syntax error and I do not know where.

            Comment

            • Jordan79
              New Member
              • Mar 2008
              • 20

              #21
              This is fixed and working now
              Thanks for all your help

              Here is the working code

              [PHP]$sql = "SELECT `user_id` FROM `user` WHERE `username` = '".$_SESSION['MM_Username']."'";
              $queryresult = mysql_query($sq l)
              or die (mysql_error()) ;
              if (mysql_num_rows ($queryresult) > 0) {
              $row=mysql_fetc h_assoc($queryr esult);
              $userid=$row['user_id'];
              }

              $resins=mysql_q uery("INSERT INTO gallery (`user_id`, `image_name`) VALUES ('$userid','$v' )")
              or die("Error in INSERT: ".mysql_error() );
              [/PHP]

              Thanks again..it wouldnt be working with out u :)

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #22
                Good that it works now.

                Remember, you cannot use quotation marks ina MySQL statement for enclosing table, column or db names. You have to use 'back ticks' for that. And you found that out.

                See you again some time.

                Ronald

                Comment

                Working...