我正在使用登录表单对HTML和PHP界面进行编码,以便可以连接到mySQL数据库并进行SELECT查询。
在表格中输入信息并单击“登录”后,我希望PHP以“ root”身份连接到我的数据库,并查看输入的信息是否属于该数据库的用户。
测试后,我期望mysqli_num_rows()
的结果为1,但结果为0。在执行过程中,遇到以下错误:
mysqli_num_rows()期望参数1为mysqli_result ...
此外,同时使用用户名和密码变量的printf()
时,结果为空。
这是我现在正在使用的两个文件的代码段。
auditoria.php
<body>
<form method="POST" action="login111.php">
<div id="form_wrapper">
<div id="form_left">
<img src="database.png" alt="app icon">
</div>
<div id="form_right">
<h1>Auditor Authentication</h1>
<div class="input_container">
<i class="fas fa-envelope"></i>
<input placeholder="Username" type="username" name="Username" id="field_username" class='input_field'>
</div>
<div class="input_container">
<i class="fas fa-lock"></i>
<input placeholder="Password" type="password" name="Password" id="field_password" class='input_field'>
</div>
<input type="submit" value="Login" name="Submit" id='input_submit' class='input_field' >
</div>
</div>
</form>
</body>
login111.php
<?php
session_start(); // Starting Session
$error = ''; // Variable To Store Error Message
if (isset($_POST['Submit'])) {
if (empty($_POST['Username']) || empty(MD5($_POST['Password']))) {
$error = "Username or Password is invalid";
}
else{
// Define $username and $password
$username = $_POST['Username'];
$passwordMd5 = md5($_POST['Password']);
$password = $_POST['Password'];
printf("username: .\n",$username);
printf("pass encriptada: .\n",$passwordMd5);
printf("pass:.\n",$password);
// mysqli_connect() function opens a new connection to the MySQL server.
$conn = mysqli_connect("localhost", "root", "", "log");
// SQL query to fetch information of registerd users and finds user match.
$query = "SELECT IDAuditor, NomeUtilizador, Password FROM auditor";
printf("Query: \n",$query);
$result = mysqli_query($conn, $query);
$num=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$num);
// To protect MySQL injection for Security purpose
if ($num>0) {
readfile("CONAN.html");
$conn2 = mysqli_connect("localhost", $username , $password, "log");
} else {
mysqli_close($conn); // Closing Connection
}
}
}
?>
错误: