I have updated the code and simplify it as much as I can. It seems like running mysql_query for the updates is not working at all.
I reproduce the current updated code:
<?php
// Get access by using ispconfig default
require_once '/usr/local/ispconfig/interface/lib/config.inc.php';
// I think the following is only needed if this is run from control panel
// require_once '/usr/local/ispconfig/interface/lib/app.inc.php';
// Create and check connection to proceed
$ip_updater = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_password']);
if (!$ip_updater) {
echo "Connection failed! \r\n"; die(mysql_connect_error());
} else {
// Connection is fine. Select the database
$db_selection = mysql_select_db($conf['db_database'], $ip_updater);
if (!$db_selection) {
echo "Can\'t use selected database! \r\n"; die(mysql_error());
} else {
// Databse is selected. Get public ip. Use others if this not working.
$public_ip = file_get_contents('http://phihag.de/ip/');
if (!filter_var($public_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true) {
echo "Filtering failed!"; die (mysql_error());
} else {
// IPv4 is true. Extract stored ip.
echo "Comparing public IP " . $public_ip . " with ";
$select_ip = "SELECT ip_address FROM server_ip WHERE sys_userid =1";
$query_ip = mysql_query($select_ip);
list ($stored_ip) = mysql_fetch_row($query_ip);
echo "stored IP " . $stored_ip . ". \r\n";
// Update stored ip only if there is a change
if ($public_ip == $stored_ip) {
echo "Same IP address. No changes is made." . "\r\n"; die (mysql_error());
} else {
echo "Different IP address. Attempting updates..." . "\r\n";
$update_ip = mysql_query("UPDATE [icode]dns_rr[/icode] SET [icode]data[/icode] = replace([icode]data[/icode], '.$stored_ip.', '.$public_ip.')");
$update_ip2 = mysql_query("UPDATE [icode]sys_datalog[/icode] SET [icode]data[/icode] = replace([icode]data[/icode], '.$stored_ip.', '.$public_ip.')");
$update_ip3 = mysql_query("UPDATE [icode]server_ip[/icode] SET [icode]ip_address[/icode] = replace([icode]ip_address[/icode], '.$stored_ip.', '.$public_ip.')");
}
// Check the updates mysql_query
if(!$update_ip3) {
echo "Public ip should now be the same with stored ip. :( \r\n"; die (mysql_error());
} else {
// Double check stored ip as update mysql_query always returns successful :(
$select_new_ip = "SELECT ip_address FROM server_ip WHERE sys_userid =1";
$query_new_ip = mysql_query($select_new_ip);
$fetched_new_ip = mysql_fetch_assoc($query_new_ip);
$new_stored_ip = $fetched_new_ip['ip_address'];
echo "Stored ip is ";
if ($new_stored_ip = $stored_ip) {
echo "still " . $new_stored_ip . ". Updates failed! \r\n"; die (mysql_error());
} else {
echo "now " . $new_stored_ip . ". Updates are successful! Thanks God." . "\r\n";
}
}
}
}
}
// Close connection
mysql_close($ip_updater);
echo "Closing connection..." . "\r\n";
?>
I already checked the database and its user has update privilege to update from localhost. What else could be wrong?