Problems writing to mySQL DB in PHP..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pheddy
    New Member
    • Dec 2008
    • 80

    Problems writing to mySQL DB in PHP..

    Hey everybody :D

    I have a problem writing variables to my mySQL database using PHP. I am faerly new to php, and this problem seems to keep irritating me, so please help me hehe !! Thanks :D

    When creating a var like:
    [code=php]$strSIP = $_SERVER['REMOTE_ADDR'];[/code]

    And trying to put it into the mysql_query like:
    [code=php]$query = "INSERT INTO `sys_session` (`ID`, `syslog_ip`)" .
    " VALUES ('', $strSIP)";

    $result = mysql_query( $query );[/code]
    I get the mysql_error:
    You have an error in your SQL syntax near '.1.2)' at line 1
    (I have trunk'd some of the code..)
    What is wrong here? when i print or echo the $strSIP it gives the perfekt xxx.xxx.x.x IP.. The mySQL database is set up in syslog_ip as varchar(128)..

    Thanks for any reply.
    Last edited by Atli; May 31 '09, 10:31 AM. Reason: Added [code] and [quote] tags.
  • hoopy
    New Member
    • Feb 2009
    • 88

    #2
    Hi you need to surround the variable in quotes if using characters like so:

    [code=php]$query = "INSERT INTO `sys_session` (`ID`, `syslog_ip`)" ." VALUES ('','$strSIP')" ;[/code]
    Last edited by Atli; May 31 '09, 10:32 AM. Reason: Added [code] tags.

    Comment

    • prabirchoudhury
      New Member
      • May 2009
      • 162

      #3
      hey.. if you are using php >4.0 then you have to call IP address using getenv() function

      Code:
      $strSIP = getenv('REMOTE_ADDR');
      may be your sql insert ip field injecting empty..


      look on PHP: getenv - Manual


      :)

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by prabirchoudhury
        hey.. if you are using php >4.0 then you have to call IP address using getenv() function

        Code:
        $strSIP = getenv('REMOTE_ADDR');
        may be your sql insert ip field injecting empty..


        look on PHP: getenv - Manual


        :)
        Using $_SERVER is perfectly fine, and, as his first post shows, the ip address is being populated.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by prabirchoudhury
          hey.. if you are using php >4.0 then you have to call IP address using getenv() function
          That's just... not true.

          Using $_SERVER['REMOTE_ADDR'] works fine in all versions later than 4.1.
          Prior to that $HTTP_SERVER_VA RS['REMOTE_ADDR'] was used

          Comment

          • Pheddy
            New Member
            • Dec 2008
            • 80

            #6
            Originally posted by hoopy
            Hi you need to surround the variable in quotes if using characters like so:

            [code=php]$query = "INSERT INTO `sys_session` (`ID`, `syslog_ip`)" ." VALUES ('','$strSIP')" ;[/code]
            Using quotes around the variable, was the answer here :D Thank you!

            Comment

            Working...