我一直在试图编写一个PHP程序,使用户能够注册一个帐户和登录后才能登录。我已经设法阻止多个用户名的注册,并将所有注册的用户名和相应的密码存储在一个SQL表中,但我试图通过验证密码来登录工作,并且PHP似乎没有从SQL中识别任何东西。我试过mysqli_fetch_array
和mysqli_fetch_assoc
无数的变化无济于事。 PHP代码的全部内容在下面发布,其中注释说明了最相关的部分。从SQL打印到PHP?
<?php session_start(); ?>
<html>
<head>
<title>Test Form</title>
</head>
<body>
<link rel="stylesheet" type="text/css" href="main.css" />
<?php
include('mydbinfo.php');
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
echo("<font color='red'><p>Unable to connect to the database system</font>"."<font color='red'>Please try later.</font></p>");
exit();
}
if($_POST["email"].value == "" or $_POST["password"].value == "") {
print("<font color='red'><p>Sorry, you must enter values into each field.</font></p>");
} else {
$mail = $_POST["email"];
$_SESSION["sessUsername"] = $mail;
$pass = $_POST["password"];
$query = "SELECT user_password FROM logins WHERE email='$mail'";
$result=mysqli_query($conn,$query);
while($name = mysqli_fetch_array($result)) {
array_push($names,$name['email']);
for($n = 0; $n < count($names); $n++) {
if($names[$n] == $name['email']) {
$names++;
}
}
}
//Number of instances of SQL query result found in table.
$count = mysqli_num_rows($result);
if($count > 0) {
//Supposed to contain password which corresponds to a given user ID.
//This is the main issue.
$new=mysqli_fetch_array($result);
if($pass == $new) {
print "<font color='red'><p>Welcome back, $mail!</p></font>";
} else {
print "<font color='red'>Your reservation was invalid.</font>";
print($pass);
print($result);
}
} else {
echo "<font color='red'>Not found in table.</font>";
}
}
?>
</body>
</html>
<?php>
螺纹在代码块中不起作用。 – GolezTrol
我认为用粗体代码不是一个好主意。也许你可以在之前添加一条评论来说它是与问题最相关的部分,但是它并不能使用stackoverflow的代码格式进行读取。 – nha
哇,这是令人尴尬的。它是“//在表中找到的SQL查询结果的实例数。”到最后才是真正有意义的,有没有其他方式可以表明这一点? – eversomber