malformed SQL Query maybe?

Discussion in 'PHP' started by advancedfuture, Dec 2, 2007.

  1. #1
    
    mysql_connect("x.x.x.x"," xxxxxx ","xxxxxxxxx");
    
    $query = "INSERT INTO users (video_url) VALUES ('$path') WHERE username = 'test';"
    mysql_query($query) or trigger_error("FATAL ERROR: " . mysql_error(), E_USER_ERROR);
    mysql_close();
    
    Code (markup):
    I get an error on the last line that says:

    Parse error: parse error, unexpected T_STRING in videoUpload.php on line 31

    Is my SQL statement incorrect?
     
    advancedfuture, Dec 2, 2007 IP
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You are missing a semi-colon after the following line, actually you included it in the SQL query:
    
    $query = "INSERT INTO users (video_url) VALUES ('$path') WHERE username = test;"
    
    Code (markup):
     
    Evoleto, Dec 2, 2007 IP
  3. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #3
    Hi,

    below are the errors that i see

    $query = "INSERT INTO users (video_url) VALUES ('$path') WHERE username = test;"

    enclose test in quotes ' '
    remove the semi-colon (;) at the end of test
     
    serialCoder, Dec 2, 2007 IP
  4. advancedfuture

    advancedfuture Banned

    Messages:
    481
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well I fixed that error by writing it:

    $query = "INSERT INTO users (video_url) VALUES ('$path') WHERE username = 'test'";
    mysql_query($query) or trigger_error("FATAL ERROR: " . mysql_error(), E_USER_ERROR);

    But now it's giving me this error:

    FATAL 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 'WHERE username = 'test'' at line 1 in videoUpload.php on line 33
     
    advancedfuture, Dec 2, 2007 IP
  5. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #5
    darn, why i didnt i see it

    INSERT operations dont need a WHERE statement, please remove it

    $query = "INSERT INTO users (video_url) VALUES ('$path')";
     
    serialCoder, Dec 2, 2007 IP
  6. advancedfuture

    advancedfuture Banned

    Messages:
    481
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #6
    my database has 4 columns

    user_id, username, password, video_url


    Now at first the video_url field is blank.. thats why I am using a WHERE... so when the user uploads a video to the server it enters the URL path in that field... if i didnt use WHERE it would just insert the statement anywhere and wouldnt be tagged to a specific user.

    Am I trying to accomplish this the incorrect way?
     
    advancedfuture, Dec 2, 2007 IP
  7. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #7
    yep, you should probably insert the path along with the username or any other informations that your need to include with the video

    Ex: $query = "INSERT INTO users (user_id, username, video_url) VALUES ($user_id, '$username','$path')";

    the $user_id, $username and so forth represents the data that you want to include
     
    serialCoder, Dec 2, 2007 IP
  8. advancedfuture

    advancedfuture Banned

    Messages:
    481
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Actually I solved my problem by changing my query to:
    
    $query = "UPDATE users SET video_url = '$path' WHERE username = 'test'";
    
    Code (markup):
     
    advancedfuture, Dec 2, 2007 IP
  9. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #9
    lol, i thought you were trying to insert and not update :D
     
    serialCoder, Dec 2, 2007 IP
  10. advancedfuture

    advancedfuture Banned

    Messages:
    481
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I am trying to insert but my syntax was wrong and I didn't want to monkey around with it..

    So UPDATE did the same thing, actually even better because it will append the field if the user ever decides to change their video.
     
    advancedfuture, Dec 2, 2007 IP