我已经设置了一个register.php页面来在我的网站上注册用户,当他们提交表单时,他们应该被重定向到login.php页面,如果有任何错误,它应该显示消息“出了点问题,请重试后来”。它已经在几天前工作了,但是由于某种原因它总是显示错误消息。
// Define variables and initialize with empty values
$ username = $ password = $ confirm_password = $ firstname = $ lastname = $ emailaddress = $ phonenumber = $ address =“”; $ username_err = $ password_err = $ confirm_password_err = $ firstname_err = $ lastname_err = $ emailaddress_err = $ phonenumber_err = $ address_err =“”;
//提交表单时处理表单数据 if($ _ SERVER [“ REQUEST_METHOD”] ==“ POST”){
// Validate username
if(empty(trim($_POST["username"]))){
$username_err = "Please enter a username.";
} else{
// Prepare a select statement
$sql = "SELECT id FROM users WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// Set parameters
$param_username = trim($_POST["username"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
/* store result */
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
$username_err = "This username is already taken.";
} else{
$username = trim($_POST["username"]);
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Validate password
if(empty(trim($_POST["password"]))){
$password_err = "Please enter a password.";
} elseif(strlen(trim($_POST["password"])) < 6){
$password_err = "Password must have atleast 6 characters.";
} else{
$password = trim($_POST["password"]);
}
// Validate confirm password
if(empty(trim($_POST["confirm_password"]))){
$confirm_password_err = "Please confirm password.";
} else{
$confirm_password = trim($_POST["confirm_password"]);
if(empty($password_err) && ($password != $confirm_password)){
$confirm_password_err = "Password did not match.";
}
}
// Validate first name
if(empty(trim($_POST["firstname"]))){
$firstname_err = "Please enter your first name.";
} else{
$firstname = trim($_POST["firstname"]);
}
if(empty(trim($_POST["lastname"]))){
$lastname_err = "Please enter your last name.";
} else{
$lastname = trim($_POST["lastname"]);
}
if(empty(trim($_POST["emailaddress"]))){
$emailaddress_err = "Please enter your email address.";
} else{
$emailaddress = trim($_POST["emailaddress"]);
}
if(empty(trim($_POST["phonenumber"]))){
$phonenumber_err = "Please enter your phone number.";
} else{
$phonenumber = trim($_POST["phonenumber"]);
}
if(empty(trim($_POST["address"]))){
$address_err= "Please enter your address.";
}else{
$address=trim($_POST["address"]);
}
// Check input errors before inserting in database
if(empty($username_err) && empty($password_err) && empty($confirm_password_err) && empty($firstname_err) && empty($lastname_err) && empty($emailaddress_err) && empty($phonenumber_err) && empty($address_err))
{
// Prepare an insert statement
$sql = "INSERT INTO users (username, password, firstname, lastname, emailaddress, phonenumber, address) VALUES (?, ?, ?, ?, ?, ?,?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sssssis", $param_username, $param_password, $param_firstname, $param_lastname, $param_emailaddress, $param_phonenumber, $param_address);
// Set parameters
$param_username = $username;
$param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
$param_firstname = $firstname;
$param_lastname = $lastname;
$param_emailaddress = $emailaddress;
$param_phonenumber = $phonenumber;
$param_address= $address;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Redirect to login page
header("location: login.php");
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
} ?>