This question already has an answer here:
This seems to be a simple insert of information into the database using prepared statements & MYSQLi, yet I only get results if the data is inserted in the old manor (not secure):
$write = mysql_query("INSERT INTO stats VALUES('','$pagename','$network','$duration','$statvote','',INET_ATON('$ip'),NOW())");
I am using:
Server version: 5.6.43 - MySQL Community Server (GPL) PHP Version 5.6.40 API Extensions Enabled mysql,mysqli,pdo_mysql
I have made sure MYSQLi is present:
if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
echo 'We don\'t have mysqli!!!';
} else {
echo 'Yes we have it!';
}
....and checked on the command line with:
php -m | grep -i mysql
and received:
mysql
mysqli
mysqlnd
pod_mysql
I conneect sucessfully..
// Create connection
// servername username PW dbname
$conn = new mysqli(localhost, ***************, ***********, ************);
// Check connection
//if (mysqli_connect_error()) { echo mysqli_connect_error(); exit;}
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
The main code to insert a record is:
// Test variables
$pagename= 'Test';
$network = 'MMM';
$duration = 60;
$statvote = 1;
$username = 'star';
$ip = $_SERVER['REMOTE_ADDR']; ///gets visitor IP address
// the ??????? below are parameter markers used for variable binding..this is a Template
$write = "INSERT INTO stats (showname,network,duration,vote,username,ip,timevoted) VALUES(?,?,?,?,?,INET_ATON(?),NOW())";
//prepare
$stmt = $conn->prepare($write);
// Bind
$stmt-> bind_param("ssiisss",$pagename,$network,$duration,$statvote,$username,INET_ATON($ip),NOW());
$stmt->execute();
$stmt->close();
$conn->close();
I expected a record to be placed onto the end of the database, but I receive no error messages($stmt->error and mysqli_error($stmt),.. almost as if the code is not there. I have checked the site, and have seen discussions that I thought were solutions, like not including the "Id" in the prepared statement ,using INET_ATON(?) in the prepared statement, and making sure the variable types were correct(s,i,d,b). But my results were the same.
Is there anything else wrong with my specific code?
Thanks
</div>