我试图从表单验证一些登录信息。安全性并不重要,因为这对我的朋友和我来说只是一个小小的社交媒体,我有备份,所以我不介意被黑客入侵,我只是在学习php。
以下是我遇到的错误:
致命错误:在非对象上调用成员函数fetch_assoc() 在/srv/disk4/2177948/www/website.com/postlogin.php第34行
这是代码: 第34行标有//
注意:连接信息已被删除,它在脚本中运行良好。
<?php
session_start();
$usrName = $_POST['logInName'];
$pass = $_POST['logInPass'];
$servername = "censored";
$username = "censored";
$password = "censored";
$db_name = "censored";
$conn = mysqli_connect($servername, $username, $password, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT usrName FROM loginInfo WHERE usrName='" . $usrName . "';";
$result = mysqli_query($conn,$sql);
if ($conn->query($sql) == TRUE) {
while ($row = $result->fetch_assoc()) {
$retrievedUsrName = $row["usrName"];
}
} else {
echo 'Error: ' . $conn->error;
}
$result->free;
if ($retrievedUsrName == $usrName) {
$sql2 = "SELECT pw FROM logininfo WHERE pw='" . $pass . "';";
$result2 = mysqli_query($conn,$sql2);
while ($row = $result2->fetch_assoc()) { //Line 34
$retrievedPass = $row["pw"];
}
} else {
echo 'Fant ikke brukernavnet i databasen. Spør Tommy.';
}
if ($retrievedPass == $pass) {
header('Location: index.php');
} else {
echo 'Fant ikke passordet i databasen. Spør Tommy.';
}
$result->free;
$conn->close;
?>