Php 错误消息未正确显示
当我点击注册时,我总是会收到两条错误消息:注册成功!和注册失败:/ 我总结了errors.php文件中的错误:Php 错误消息未正确显示,php,mysql,Php,Mysql,当我点击注册时,我总是会收到两条错误消息:注册成功!和注册失败:/ 我总结了errors.php文件中的错误: <?php if(count($errors) > 0): ?> <div class="error"> <?php foreach ($errors as $error): ?> <p><?php echo $error; ?></p> <?p
<?php if(count($errors) > 0): ?>
<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach ?>
</div>
<?php endif ?>
我修改了您的MySQL查询以准备语句,而且您还有两个isset of submit,这就是问题所在。。。
试试这个
//然后换个按钮
从…起
登记
到
永远不要存储纯文本密码!请使用PHP来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用密码\u hash()
。在散列之前,不需要对它们执行任何操作或使用任何其他清理机制。这样做会更改密码并导致不必要的额外编码。表示了解的语句。即使是这样也不安全!您真的不应该在注册时使用,新值是否显示在数据库中?这些值不会显示在数据库中。您有一个额外的“}”,我删除了它。但是,当我运行代码时,如果我将一个或两个输入字段都留空,则不会收到任何错误消息。数据库“registration”中的表“users”中没有显示任何值。ID、电子邮件和密码。您没有在fistAdd指定以下NOP更改insert语句,如下所示
<?php
//========================================
// Default values. Used for error messages
//========================================
$Email = "";
$errors = array();
//===============
// MySQL Settings
//===============
define('DB_NAME', 'registration');
define('DB_USER', 'root');
define('DB_PASSWORD', '12345678');
define('DB_HOST', 'localhost');
//====================
// Database connection
//====================
//Connect to server
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " . mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
//===================================
// If the Register button is clicked
//===================================
if(isset($_POST['Register'])) {
$Email = mysqli_real_escape_string($_POST['Email']);
$Password = mysqli_real_escape_string($_POST['Password']);
//=================================
// Error messages if input is wrong
//=================================
if(empty($Email)) {
array_push($errors, "Email is required");
}
if(empty($Password)) {
array_push($errors, "Password is required");
}
//=========================
// Send data to MySQL table
//=========================
// Send input form data to MySQL database
if(isset($_POST['Register'])){
$Email = $_POST['Email'];
$Password = $_POST['Password'];
// If no error messages appear, then send data to table
if(count($errors) == 0){
$Password = md5($Password);
$sql = "INSERT INTO users (Email, Password) VALUES ('{$Email}', '{$Password}')";
}
$result = mysqli_query($link, $sql);
if ($result) {
// Success
echo "Registration successful!";
} else {
// Failure
echo "Registration failed :/";
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>
<form method="post" action="Register.php">
<!--Display errror messages here-->
<?php include('Errors.php'); ?>
<div class="input-group">
<label>Email</label>
<input type="text" name="Email">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="Password">
</div>
<div class="input-group">
<button type="submit" id="button" name="Register" class="Button">Register</button>
</div>
<p>
Already a member? <a href="Login.php">Sign in</a>
</p>
</form>
</body>
</html>
<?php
// Close database connection
if (isset($link)) {
mysqli_close($link);
}
?>
I modified your MySQL query to prepare statement and also you had two isset of submit which is the problem...
Try this
<?php
//======================================== // Default values. Used for error messages //======================================== $Email = ""; $errors = array();
//=============== // MySQL Settings //===============
define('DB_NAME', 'registration'); define('DB_USER', 'root');
define('DB_PASSWORD', '12345678'); define('DB_HOST', 'localhost'); //==================== // Database connection //==================== //Connect to server
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Test if connection succeeded
if(mysqli_connect_errno()) { die("Database connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ")" ); } //=================================== // If the Register button is clicked //=================================== if(isset($_POST['Register'])) { $Email = $_POST['Email']; $Password = $_POST['Password']; //================================= // Error messages if input is wrong //=================================
if(empty($Email)) {
array_push($errors, "Email is required");
} elseif(empty($Password)) {
array_push($errors, "Password is required");
} //========================= // Send data to MySQL table //========================= // Send input form data to MySQL database
else{
$Email = $_POST['Email'];
$Password = md5($_POST['Password']);
// If no error messages appear, then send data to table
$sql = "INSERT INTO users (Email, Password) VALUES (?,?)";
$stmt = $link->prepare($sql);
$stmt->bind_param('ss',$Email,$Password);
$stmt->execute();
if ($stmt) {
// Success
echo "Registration successful!";
} else {
// Failure
echo "Registration failed :/";
} } } ?>
//And change the button
from
<button type="submit" id="button" name="Register" class="Button">Register</button>
To
<input type="submit" id="button" value="Register" name="Register" class="Button">