I am trying to create a PHP function to update the SQL table to keep from reusing code over and over. However, whenever I run the function, I always get 'SQL database error.' The odd part about this is I do not get any type of error whenever I use copy/paste this snippet of code and put it by itself, not as a function. What is wrong? Why is it behaving this way?
$servername = "localhost";
$dBUsername = "username";
$dBPassword = "password";
$dBName = "databasename";
$connection = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
function updateSQL ($table, $row, $value, $where, $arg) {
$SQL = '
UPDATE `".$table."`
SET `".$row."`=?
WHERE `".$where."`=?';
$stmt = mysqli_stmt_init($connection);
if (!mysqli_stmt_prepare($stmt, $SQL)) {return "SQL database error."; exit();}
else {
mysqli_stmt_bind_param($stmt, "ss", $value, $arg);
if (mysqli_stmt_execute($stmt)) {return "success";}
else {return mysqli_error($connection);}
}
}