任何人都可以帮我解决这个代码到底出错的地方吗?当我试图检查数据库中是否已存在电子邮件时,我错了。
如何检查数据库中是否存在电子邮件?
<html>
<h1> Registration Form </h1>
<body>
<form method="post" action="">
<input type="text" name="fname" placeholder="first name" required><br><br>
<input type="text" name="lname" placeholder="last name" required><br><br>
<input type="text" name="mail" placeholder="mail" required> <br><br>
<input type="PASSWORD" name="pass1" placeholder="password" required> <br><br>
<input type="PASSWORD" name="pass2" placeholder="repeat password" required> <br> <br>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['mail'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($pass1 !== $pass2)
{
echo "password in correct ";
}
else
{
$db = mysqli_connect("localhost","root","","reviewsystem");
$check = "SELECT * FROM registers WHERE $mail = email";
if($check == TRUE){
echo "email already exists";
}
else{
$query = "INSERT INTO registers(fname,lname,email,password) VALUES('$fname','$lname','$mail','$pass1')";
mysqli_query($db,$query);
echo " you are registered succesfully";
}
}
}
?>
</body>
</html>
答案 0 :(得分:1)
首先要做的是:您没有执行任何类型的查询。你刚刚创建了一个字符串。添加一行:
$db = mysqli_connect("localhost","root","","reviewsystem");
$check = "SELECT * FROM registers WHERE $mail = email";
$result = mysqli_query($db, $check); //i think there is a typo, mysqi_query
之后,您可以使用mysqli_num_rows返回从DB返回的SELECT行数,以确定该条目是否已存在:
if(mysqli_num_rows($result) > 0){
echo "email already exists";
}
答案 1 :(得分:0)
问题出在你的mysql查询的where子句中。你必须把它写成
$check = "SELECT * FROM registers WHERE email = '".$mail."'"; if($check == TRUE){ echo "email already exists"; } else{