gpt4 book ai didi

php - 如何检查用户是否已存在于 SQL 数据库中?

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:20 26 4
gpt4 key购买 nike

<分区>

我正在尝试为类(class)项目测试一个简单的用户帐户注册页面,并且我正在尝试创建一个检查,如果他们的电子邮件已经在数据库中,将通知用户,因此不会将它们添加到其中再次。这是我的 PHP 代码。

    <?php
$collegeid = mysql_real_escape_string('1');
$email = mysql_real_escape_string('abc@test.com');
$password = mysql_real_escape_string(md5('test1'));
$name = mysql_real_escape_string('Test Test');
$bday = mysql_real_escape_string('1900-01-01');
$class = mysql_real_escape_string('Freshman');
//echo "<p>Test</p>";
$servername = "localhost";
$username = redacted;
$serverpassword = redacted;
$dbname = redacted;

$conn = new mysqli($servername, $username, $serverpassword, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$checkquery = "SELECT * FROM Student WHERE Email = '$email'";
$insertquery = "INSERT INTO Student (CollegeID, Name, Birthday, Email, Classification, Password) VALUES ('$collegeid', '$name', '$bday', '$email', '$class', '$password')";

if (mysql_num_rows($conn->query($checkquery)) > 0)
{
echo "Error: Email already in use";
}

else
{
$conn->query($insertquery);
echo "Account Created.";
}

?>

但是,无论该用户是否在数据库中,它总是告诉我帐户已创建。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com