我目前正在学习如何在Udemy开发动态网站。我已经阅读了stackoverflow上的每个以前类似的线程,似乎没有工作。可能是因为我遵循了教练的脚本脚步。我面临的唯一问题是检查电子邮件是否已经在数据库表中。我认为问题出在"SELECT * FROM users WHERE email='$email'"
。
任何帮助都表示赞赏,如果我的问题无论如何重复出现同样的问题,请指导我进入该主题。
整个代码:
<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "register") or die("error happened");
?>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$pass = mysqli_real_escape_string($conn, $_POST['pass']);
if(isset($_POST['signup'])){
if ($name == "" OR $email == "" OR $pass == ""){
echo "<script>alert('Please enter required fields.');</script>";
exit;
}
$sel_email = "SELECT * FROM users WHERE email = ".mysqli_real_escape_string($conn, $_POST['email'])." ";
$run_email = mysqli_query($conn, $sel_email);
$check_email = mysqli_num_rows($run_email); // The error that always gets displayed.
if($check_email == 1){
echo "<script>alert('This e-mail has been registered already.');</script>";
} else {
$_SESSION['email'] = $email;
$insert = "INSERT INTO users
(name, email, password) VALUES ('$name', '$email', '$pass')";
$run_insert = mysqli_query($conn, $insert);
if($run_insert){
echo "<script>alert('Thank you fro signing up!');</script>";
}
}
}
/*===============================
Login part ================================*/
if(isset($_POST['login'])){
$sel = "SELECT * FROM users WHERE email='$email' AND password='$pass'";
$run = mysqli_query($conn, $sel);
$check = mysqli_num_rows($run);
if($check == 1){
echo "<script>alert('Email is not registered. Please register and then sign up.')</script>";
} else {
$_SESSION['email'] = $email;
echo "<script>window.location.href = 'home.php';</script>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
</head>
<body>
<div class="regis pull-right">
<div class="container">
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-3">
<form class="text-center" action="" method="post">
<div class="form">
<h3>Sign up</h3>
<div class="form-group">
<input type="text" class="name" name="name" placeHolder="Enter your name" />
</div>
<div class="form-group">
<input type="text" class="email" name="email" placeHolder="Enter your e-mail" />
</div>
<div class="form-group">
<input type="password" class="pass" name="pass" placeHolder="Enter your password" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" name="signup" value="Sign up" />
</div>
</div>
</form>
</div>
<div class="col-md-3">
<form class="text-center" action="" method="post">
<div class="form">
<h3>Login</h3>
<div class="form-group">
<input type="text" name="emailLogin" placeHolder="Enter your email" />
</div>
<div class="form-group">
<input type="password" name="passLogin" placeHolder="Enter your password" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" name="login" value="Log in" />
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
</body>
</html>
答案 0 :(得分:1)
问题出在这一行:
WHERE email = ".mysqli_real_escape_string($conn, $_POST['email'])." ";
缺少单引号。
WHERE email = '".mysqli_real_escape_string($conn, $_POST['email'])."' ";
但是,您已将其分配到:
$email = mysqli_real_escape_string($conn, $_POST['email']);
所以只做:(并且更简单)
WHERE email = '".$email."' ";
检查了您的查询错误后,就会发出信号。
将or die(mysqli_error($conn))
添加到mysqli_query()
。
即:
$run_email = mysqli_query($conn, $sel_email) or die(mysqli_error($conn));
另外,您没有发布HTML表单,因此很难说您的POST数组是否包含值,表单是否有POST方法以及所有输入是否具有相同的名称属性。 / p>
即:<input type="text" name="email">
等。
将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:只应在暂存时进行显示错误,而不是生产。
<强>密码强>
我还注意到您可能以纯文本格式存储密码。不建议这样做。
使用以下其中一项:
crypt()
bcrypt()
scrypt()
password_hash()
功能。其他链接:
答案 1 :(得分:0)
该错误显示因为,查询返回zero / 0
个值。
换句话说,返回为假(布尔值)。
添加一些像这样的过滤器
if($run_email)
$check_email = mysqli_num_rows($run_email);
else
die(mysqli_error($conn));