Hello!
I have a database table which contains an unsigned integer field to store the visitor's IP address:
`user_ip` INT(10) UNSIGNED DEFAULT NULL,
Here's the snippet of PHP code which tries to store the IP address:
$ipaddr = $_SERVER['REMOTE_ADDR'];
if ($stmt = mysqli_prepare($dbconn, 'INSERT INTO visitors(user_email, user_ip) VALUES (?,?)'))
{
$remote_ip = "INET_ATON('$ipaddr')";
mysqli_stmt_bind_param($stmt, 'ss', $email, $remote_ip);
if (mysqli_stmt_execute($stmt) === FALSE) return FALSE;
$rows_affected = mysqli_stmt_affected_rows($stmt);
mysqli_stmt_close($stmt);
}
The INSERT operation succeeds, however the user_ip field contains a null value.
I have also tried changing the parameter type in mysqli_stmt_bind_param() (which was set to string in the above example) to integer, i.e. mysqli_bind_param(... 'si',...) - but to no avail.
I've also tried using the following bit of code instead of mysql's INET_ATON() SQL function:
function IP_ATON($ipaddr)
{
$trio = intval(substr($ipaddr,0,3));
return ($trio>127) ? ((ip2long($ipaddr) & 0x7FFFFFFF) + 0x80000000) : ip2long($ipaddr);
}
It still doesn't work - the 'user_ip' field is still set to null. I've tried passing the $ip_addr variable as both integer & string in mysqli_bind_param() - to no avail.
It seems the problem lies with the parameterized insert.
The following "old-style" code works without any problem:
mysqli_query(..., "INSERT INTO visitors(user_email, user_ip) VALUES ('$email',INET_ATON('$ipaddr'))");
What am I doing wrong here?
Thanks in advance!