我正在尝试从数据库表中删除一行,而我的msqli预处理语句返回false。我错误检查了$ commentId并正确地传递了它。但我找不到它不起作用的原因。
请帮助!
<?php
session_start();
include('connect.php');
if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == true && $_SESSION['id'] == 43) {
DeleteRowFunction($_POST['commentId']);
} else {
echo "You are not authorized to delete comments";
}
function DeleteRowFunction($commentId) {
//$deleteComment = "DELETE FROM `login`.`commenttable` WHERE `commenttable`.`id` = ?";
$deleteComment = "DELETE FROM commenttable WHERE id = ?";
if ($stmt = mysqli_prepare($connection, $deleteComment)) {
mysqli_stmt_bind_param($stmt, "s", $commentId);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
} else {
echo "error";
}
}
?>
答案 0 :(得分:0)
您未在功能中设置$connection
变量。如果启用了错误报告,您会看到有关正在使用的未定义变量的警告。
我认为它是在connect.php
中设置的,但全局变量在函数内部不可见。您必须将其作为参数传递给函数:
DeleteRowFunction($connect, $_POST['commentId']);
...
function DeleteRowFunction($connection, $commentId) {
...
}
或者您需要将global $connection;
放在DeleteRowFunction()
的开头。