0

I'm getting this error:

Invalid query: 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 'INET_ATON('188.92.x.x')' at line 1

While trying to insert IP Address in database. The column type is:

'LastIP int(10) unsigned NOT NULL,'.

The function to execute the query is:

function onNewUser($ip, $hostname, $con)
{
    $query =    "INSERT INTO tableMachine (LastIP, LastHostName) VALUES ".
                "INET_ATON('".mysql_real_escape_string($ip, $con)."'), ".
                "'".mysql_real_escape_string($hostname, $con)."'";

    $result= mysql_query($query, $con);
    if (!$result) {
        die('Invalid query: ' . mysql_error());


    }
    }

I call this function with the parameters:

$ip = $_SERVER['REMOTE_ADDR'];

$hostname = @gethostbyaddr($ip);

onNewUser($ip, $hostname, $con);

What's wrong with it guys?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1526124
  • 229
  • 4
  • 20
  • Read http://stackoverflow.com/a/60496/2864740 - it'll make the code safer, more robust, *and* easier to see what the *actual* SQL looks like. – user2864740 Nov 05 '13 at 19:48
  • 1
    The `mysql_*` functions are deprecated in PHP 5.5. It is not recommended for writing new code as it will prevent you from upgrading in the future. Instead, use either `MySQLi_*` or `PDO` – bear Nov 05 '13 at 19:48

2 Answers2

4

your values list should be encapsulated inside of parenthesis if I am not mistaken

David Wilkins
  • 574
  • 4
  • 19
0

You should try this :

$query =    "INSERT INTO tableMachine (LastIP, LastHostName) VALUES (".
            "INET_ATON('".mysql_real_escape_string($ip, $con)."'), ".
            "'".mysql_real_escape_string($hostname, $con)."')";

I just add parenthesis for VALUES(...)

Also, as @Shamil said, the functions mysql_* are depricated. You should use mysqli_*This link should help you with the mysqli_* functions.

Gabriel L.
  • 4,678
  • 5
  • 25
  • 34