【发布时间】:2021-02-23 06:30:59
【问题描述】:
所以我试图将我的所有 SQL 语句转换为准备好的语句等,以防止 SQL 注入攻击,但我在获取东西等方面遇到了一些问题
我的代码:
if($_GET["action"] == "ban"){
if(isset($_GET["username"])){
$username = $_GET["username"];
$banMsg = $_GET["banMsg"];
$email = "test@gmx.ch";
$sql = "SELECT * FROM bans WHERE username = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->fetch();
$stmt->close();
if($result->num_rows > 0){ //LINE 61
die(json_encode(array("status" => 400, "message" => "User already banned")));
}
$result2 = $db->prepare("INSERT INTO bans (username, ip, email, message, expire, ban_creator) VALUES (?, ?, ?, ?, ?, ?)");
$result2->bind_param("sssssd", $username, null, $email, $banMsg, null, 1); // LINE 72^^
$result2->close();
if($result2){
updateBanCache();
die(json_encode(array("status" => 200, "message" => "Successfully banned")));
} else {
die(json_encode(array("status" => 400, "message" => "SQL error")));
}
}
另外,$result = $stmt->get_result(); 不想为我工作,不过我的 php / cpanel 中确实安装了 mysqlnd 驱动程序。
任何指针都会有所帮助,谢谢!
错误日志:
[11-Nov-2020 04:46:04 America/New_York] PHP Notice: Trying to get property 'num_rows' of non-object in /home/public_html/index.php on line 61
[11-Nov-2020 04:46:04 America/New_York] PHP Fatal error: Uncaught Error: Cannot pass parameter 3 by reference in /home/elysianmenu/public_html/index.php:72
Stack trace:
#0 {main}
thrown in /home/public_html/index.php on line 72
旁注:我也尝试使用$result = $stmt->get_result();,但最终出现错误:
[11-Nov-2020 04:57:30 America/New_York] PHP Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::get_result() in /home/public_html/index.php:55
Stack trace:
#0 {main}
thrown in /home/public_html/index.php on line 55
^^ 是的,我确实安装了 mysqlnd 驱动程序
【问题讨论】:
-
您对
$result2->bind_param("sssssd"的调用表明有 6 个值,而您只通过了 5 个。还要检查您的类型是否正确。 -
我得到的错误是:[11-Nov-2020 04:23:55 America/New_York] PHP Notice: Trying to get property 'num_rows' of non-object in /home/public_html/ forums/index.php 第 60 行 [2020 年 11 月 11 日 04:23:55 America/New_York] PHP 致命错误:未捕获的错误:调用 /home/public_html/forums//index 中布尔值的成员函数 bind_param() .php:71 堆栈跟踪:#0 {main} 在第 71 行的 /home//public_html/forums//index.php 中抛出,这就是我得到第 71 行的错误
-
这些细节应该是问题的一部分。
-
对不起,我忘记了,编辑并添加了它
-
还注意到我忘记从查询更改为准备,现在这样做了,现在我得到了新的错误:[11-Nov-2020 04:46:04 America/New_York] PHP Notice: Trying to get /home/public_html/forums/index.php 第 61 行 [11-Nov-2020 04:46:04 America/New_York] PHP 致命错误:未捕获错误:无法通过引用传递参数 3 中非对象的属性“num_rows”在 /home/public_html/forums/index.php:72 堆栈跟踪:#0 {main} 在第 72 行的 /home/public_html/forums/index.php 中抛出
标签: php