我一直在試圖編寫一個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