Results 1 to 24 of 24
  1. #1

    Parse error: parse error, unexpected T_VARIABLE

    Hello,
    I get the error Parse error: parse error, unexpected T_VARIABLE about this part of my script;
    PHP Code:
    $sql5="INSERT INTO $login_logs_tbl (user_id, ip, logged_date, logged_timestamp) VALUES("$vuserid[0]", "$_SERVER['REMOTE_ADDR']", $date$timestamp)";
    $result5=mysql_query($sql5); 
    Can you see what's causing the error? I've tried but I can't figure out what...

    Best Regards
    Oskar

  2. #2
    Join Date
    Sep 2004
    Posts
    105
    You're missing the string concatenation character (the period) after the quotes.

    So either
    PHP Code:
    VALUES(".$vuserid[0]."'".$_SERVER['REMOTE_ADDR']."''$date''$timestamp')"; 
    or, since you're using double quotes for $sql5, this will work too:
    PHP Code:
    VALUES({$vuserid[0]}, '{$_SERVER['REMOTE_ADDR']}''$date''$timestamp')"; 
    Note that I added single quotes around $_SERVER["REMOTE_ADDR"], $date, and $timestamp. Non-numeric values in SQL queries have to have single quotes.

  3. #3
    Join Date
    Aug 2005
    Location
    UK
    Posts
    654
    I think its teh quoting you're useing, I don't do PHP, but this looks off..
    Code:
    $sql5="INSERT INTO $login_logs_tbl (user_id, ip, logged_date, logged_timestamp) VALUES("
    It would see that as one string, then the $vuserid...

    Unless thats normal for PHP?

  4. #4
    Join Date
    Mar 2006
    Posts
    965
    PHP Code:
    $sql5="INSERT INTO $login_logs_tbl (user_id, ip, logged_date, logged_timestamp) VALUES("$vuserid[0]", "$_SERVER['REMOTE_ADDR']", $date$timestamp)"
    $result5=mysql_query($sql5); 
    for:

    PHP Code:
    $sql5"INSERT INTO $login_logs_tbl (user_id, ip, logged_date, logged_timestamp) VALUES ('$vuserid[0]', '$_SERVER['REMOTE_ADDR']', '$date', $timestamp)"
    $result5=mysql_query($sql5); 
    This should work as expected.

  5. #5
    Join Date
    Dec 2006
    Posts
    34
    If performance is an issue and the page is heavily loaded and the statement executed often you can decrease a little overhead by doing.

    PHP Code:
    $sql5='INSERT INTO '.$login_logs_tbl.' (user_id, ip, logged_date, logged_timestamp) VALUES('.$vuserid[0].', "'.$_SERVER['REMOTE_ADDR'].'", '.$date.', '.$timestamp.');';
    $result5=mysql_query($sql5); 

  6. #6
    Join Date
    Oct 2004
    Location
    UK
    Posts
    487
    Nope, not quite there yet:

    PHP Code:
    $sql5 "INSERT INTO `" $login_logs_tbl "` (`user_id`, `ip`, `logged_date`, `logged_timestamp`) VALUES ('" $vuserid[0] . "', '" $_SERVER['REMOTE_ADDR'] . "', '" $date "', '" $timestamp '")";
    $result5 = mysql_query($sql5) or die("<b>Error</b>: " . mysql_errno() . " - " . mysql_error()); 
    That's by my coding standards anyway, probably other people who would do it differently.

  7. #7
    Join Date
    Mar 2006
    Posts
    965
    PHP Code:
    $sql5 "INSERT INTO `" $login_logs_tbl "` (user_id, ip, logged_date, logged_timestamp) VALUES ('" mysql_real_escape_string($vuserid[0]) . "', '" mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . "', '" mysql_real_escape_string($date) . "', '" mysql_real_escape_string($timestamp) . "')";
    $result5 mysql_query($sql5) or die("<b>Error</b>: " mysql_errno() . " - " mysql_error()); 
    forgot to mention something on my last post. Since this forum seem to be a fan of mysql_real_escape_string, here it is above.

  8. #8
    Join Date
    Nov 2003
    Posts
    682
    Escaping is for user input. There's no need to escape REMOTE_ADDR since nothing can be injected in it.

  9. #9
    Since this forum is a fan of mysql_real_escape_string, some people are escaping strings that don't require escaping. All for the crowd, right horizon?

    Oh, I almost forgot

    Dyslexics Have More Fnu

  10. #10
    Join Date
    Mar 2006
    Posts
    965
    All for the crowd, right horizon?
    Yep, that's why I never use them in the first place.

  11. #11
    Join Date
    Dec 2006
    Location
    Kerala
    Posts
    2

    We can use in this way also

    $sql5="INSERT INTO $login_logs_tbl (user_id, ip, logged_date, logged_timestamp) VALUES('". $vuserid[0] ."', '". $_SERVER['REMOTE_ADDR'] ."', '". $date ."', '". $timestamp ."')";
    $result5=mysql_query($sql5);

  12. #12
    Join Date
    Oct 2004
    Location
    UK
    Posts
    487
    Quote Originally Posted by horizon
    PHP Code:
    $sql5 "INSERT INTO `" $login_logs_tbl "` (user_id, ip, logged_date, logged_timestamp) VALUES ('" mysql_real_escape_string($vuserid[0]) . "', '" mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . "', '" mysql_real_escape_string($date) . "', '" mysql_real_escape_string($timestamp) . "')";
    $result5 mysql_query($sql5) or die("<b>Error</b>: " mysql_errno() . " - " mysql_error()); 
    forgot to mention something on my last post. Since this forum seem to be a fan of mysql_real_escape_string, here it is above.

    I knew I would miss something! You forgot to backtick the fieldnames though:

    PHP Code:
    $sql5 "INSERT INTO `" $login_logs_tbl "` (`user_id`, `ip`, `logged_date`, `logged_timestamp`) VALUES ('" mysql_real_escape_string($vuserid[0]) . "', '" mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . "', '" mysql_real_escape_string($date) . "', '" mysql_real_escape_string($timestamp) . "')";
    $result5 mysql_query($sql5) or die("<b>Error</b>: " mysql_errno() . " - " mysql_error()); 

  13. #13
    Join Date
    Mar 2006
    Posts
    965
    You forgot to backtick the fieldnames though
    I didn't forget anything. The reason it wasn't there, is because I never use them and it works on anyhow.

  14. #14
    Join Date
    Sep 2004
    Posts
    105
    Quote Originally Posted by horizon
    I didn't forget anything. The reason it wasn't there, is because I never use them and it works on anyhow.
    You only need to backtick the field names if the field name is also a reserved word or (horrors) has a space in it.

    Is it me or is this thread a little long for the original question?

  15. #15
    Join Date
    Mar 2006
    Posts
    965
    Likely, if you wish to remove empty spaces, you can always remove them with trim.

    Is it me or is this thread a little long for the original question?
    Yes, I guess it is.

  16. #16
    Yes, removing a space from a column that has a space in its name is...just impressive horizon. Truly amazing.

    Dyslexics Have More Fnu

  17. #17
    Join Date
    Nov 2003
    Posts
    682
    Except trim() wouldn't do anything to help having a space in a column name.

  18. #18
    Join Date
    Mar 2006
    Posts
    965
    Except trim() wouldn't do anything to help having a space in a column name.
    Indeed. Trim eliminates spaces. It does not add spaces.

  19. #19
    Join Date
    Nov 2003
    Posts
    682
    Right...so why were you suggesting trim() when it has nothing to do with the problem?

  20. #20
    Join Date
    Mar 2006
    Posts
    965
    I simply replied to this sentence:

    You only need to backtick the field names if the field name is also a reserved word or (horrors) has a space in it.

  21. #21
    Join Date
    Nov 2003
    Posts
    682
    Hence my question, what does that have to do with trim()?

  22. #22
    Join Date
    Mar 2006
    Posts
    965
    What does trim() have to do with that? Nothing. Trim will not do anything to fix a column name with a space in it.
    Since this topic seem to lead to critisim, I'd rather not reply any further on this subject.

  23. #23
    Join Date
    Nov 2003
    Posts
    682
    People criticize your code in hopes that you'll read the PHP manual

  24. #24
    Join Date
    Mar 2006
    Posts
    965
    Heh, people criticize your code in hopes that you'll read the PHP manual one day
    As far as I'm concerned, I'm already doing so.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •