整理数据后我的注册表应该提交给 register.php,这是执行将数据保存在数据库中并将用户定向到 login.php 页面的过程,用户在该页面中输入用户名和密码进行登录。但是当用户输入他们的用户名和密码时,页面变为空白。我没有看到服务器记录任何错误,但 php.ini 用于显示错误。以下是代码。
输入表格;
<form action="register.php" method="post" enctype="multipart/form-data">
<div class="mb-3">
<label for="firstName" class="form-label">First Name:</label>
<input type="text" class="form-control" id="firstName" name="firstName" required>
</div>
<div class="mb-3">
<label for="middleName" class="form-label">Middle Name:</label>
<input type="text" class="form-control" id="middleName" name="middleName">
</div>
<div class="mb-3">
<label for="lastName" class="form-label">Last Name:</label>
<input type="text" class="form-control" id="lastName" name="lastName" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone:</label>
<input type="text" class="form-control" id="phone" name="phone" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email:</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="city" class="form-label">Town/City:</label>
<input type="text" class="form-control" id="city" name="city" required>
</div>
<div class="mb-3">
<label for="username" class="form-label">Username:</label>
<input type="text" class="form-control" id="username" name="username" required>
<div class="mb-3">
<label for="userType" class="form-label">User Type:</label>
<select class="form-control" id="userType" name="userType" required>
<option value="">-- Select User Type --</option>
<option value="UserType1">UserType1</option>
<option value="UserType2">UserType2</option>
<option value="UserType3">UserType3</option>
</select>
</div>
<div class="mb-3">
<label for="password" class="form-label">Create Password:</label>
<input type="password" class="form-control" id="password" name="password" required pattern="^(?=.*[a-z])(?=.*[A-Z]).{8,}$">
<small>Password must be at least 8 characters and contain at least 1 uppercase letter.</small>
</div>
<button type="submit" class="btn btn-primary" name="register">Create Account</button>
</form>
<p>Already have an account? <a href="index.html#login-form">Login here</a></p>
来自register.php;
<?php
// Start the session
session_start();
// Get the user's details from the registration form
$firstName = $_POST['firstName'];
$middleName = $_POST['middleName'];
$lastName = $_POST['lastName'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$city = $_POST['city'];
$userType = $_POST['userType'];
text`$username = $_POST['username'];
$password = $_POST['password'];
// Connect to the database
$servername = "mserver";
$dbusername = "babyjet";
$dbpassword = "mypwd";
$dbname = "users_db";
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
// Check if the connection was successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if the email or phone number is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[0-9]{11}$/", $phone)) {
$errorMessage = "Please provide a valid email address or phone number.";
header('Location: create_account.php?error=' . urlencode($errorMessage));
exit;
}
// Check if the email or phone number already exists in the database
$stmt = $conn->prepare("SELECT * FROM allusers WHERE email = ? OR phone = ?");
if (!$stmt) {
echo "Error: " . $conn->error;
exit;
}
$stmt->bind_param("ss", $email, $phone);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$errorMessage = "The email address or phone number provided is already in use. Please try again.";
header('Location: create_account.php?error=' . urlencode($errorMessage));
exit;
}
// Insert the user's details into the database
$stmt = $conn->prepare("INSERT INTO allusers (first_name, middle_name, last_name, email, phone, city, user_type, username, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
if (!$stmt) {
echo "Error:couldnt insert: " . $conn->error;
exit;
}
// Hash the password before storing it in the database
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$stmt->bind_param("sssssssss", $firstName, $middleName, $lastName, $email, $phone, $city, $userType, $username, $passwordHash);
$stmt->execute();
// Close the SQL statement and database connection
$stmt->close();
$conn->close();
// Redirect to index.php and scroll to the login form
header('Location: index.html#login-form');
exit;
?>
//登录.php;
<?php
// Start the session
session_start();
// Check if the login form was submitted
if (isset($_POST['login'])) {
// Get the user's login credentials
$username = $_POST['username'];
$password = $_POST['password'];
// Connect to the database
$servername = "myserver";
$dbusername = "babyjet";
$dbpassword = "mypsswd";
$dbname = "users_db";
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
// Check if the connection was successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare and execute the SQL statement to retrieve the user's account type
$stmt = $conn->prepare("SELECT user_type FROM allusers WHERE email=? AND password=?");
$stmt->bind_param("ss", $username, $password);
// Execute the prepared statement
$stmt->execute();
// Bind the result variables
$stmt->bind_result($user_type);
// Fetch the results
if ($stmt->fetch()) {
// Save the user's account type in the session
$_SESSION['user_type'] = $user_type;
// Redirect the user to their respective dashboard
switch ($user_type) {
case 'Patient':
header('Location: userType1_dashboard.php');
break;
case 'Nurse':
header('Location: userType2_dashboard.php');
break;
case 'Doctor':
header('Location: userType3_dashboard.php');
break;
}
exit;
} else {
// If the user's account type was not found in the database, show an error message
$errorMessage = "Invalid username or password";
}
// Close the prepared statement and database connection
$stmt->close();
$conn->close();
}`
?>
我假设您在您未提供的其他一些 php 文件中获取用户名和密码的输入。最可能的错误可能是 -
<form action="login.php" method="post" >
确保你在 action 属性和 post 方法中有 login.php。
检查表单中提交按钮的“名称”属性。确保它是“登录”。
快速查看可以使用echo查看程序的控件是否进入login.php中的if条件
我会建议-
<?php
// Start the session
session_start();
echo 'running login.php';
// Check if the login form was submitted
if (isset($_POST['login'])) {
echo 'inside if';
这将帮助您找到问题所在。