I am refractoring some of my old code and I am creating a database class with a INSERT function. For purposes of testing I have left it outside of the class and want to make it handle errors.
I want to remove the php errors and return my own errors. Currently I have the below and works fine with an insert on duplicate key but when I test removing the type argument from the bind_param I get the following error:
Warning: mysqli_stmt::bind_param(): Invalid type or no types specified in /home/matt500b/csgoberry/public/catch.php on line 17
Executing the statement failed: No data supplied for parameters in prepared statement
How can I remove the Warning issue and keep just my returned string? Or for production servers what is the best method for this type of action.
Also what other errors can I simulate with regards to mysqli queries?
function INSERT($link, $sql, $args=null) {
if ($stmt = $link->prepare($sql)) {
for($i=0; $i<count($args); $i++) {
$stmt->bind_param($args[$i]["type"], $args[$i]["value"]);
}
if(!$stmt->execute()) {
return 'Executing the statement failed: ' . htmlspecialchars($stmt->error);
exit();
}
}
else {
return 'Preparing the statement failed: ' . htmlspecialchars($link->error);
exit();
}
return 'Finished';
}
$query = "INSERT INTO insertTest (`value`) VALUES (?)";
$queryArgs[] = array("type"=>"", "value"=>"test1");
echo INSERT($link, $query, $queryArgs);