gpt4 book ai didi

php - 检查用户名和电子邮件已经存在于php mysql中

转载 作者:行者123 更新时间:2023-11-29 04:16:35 25 4
gpt4 key购买 nike

检查PHP MySQL中已经存在的用户名和电子邮件,我检查每个输入是否为空,如果没有空,则检查用户名和密码是否已在数据库中,如果不是在数据库中,则ECHO好,代码运行良好,但问题是,当新的条目被输入时,它不会打印好。
代码如下:

<?php
error_reporting(E_ALL & ~E_NOTICE);
require_once('dbcon.php');
if(isset($_POST["create"]))
{
if (isset($_POST['username']) && !empty($_POST['username'])) {
$username=mysqli_real_escape_string($conn,trim($_POST['username']));
}else{
$empty_username="Username Cannot be empty.";
echo $empty_username.'<br>';
}
if (isset($_POST['email']) && !empty($_POST['email'])) {
$email=mysqli_real_escape_string($conn,trim($_POST['email']));
}else{
$empty_email="Email cannot be empty.";
echo $empty_email.'<br>';
}

if (isset($_POST['category']) && !empty($_POST['category'])) {
$category=mysqli_real_escape_string($conn,trim($_POST['category']));
}else{
$empty_category="Category cannot be empty.";
echo $empty_category.'<br>';
}

if (isset($_POST['password']) && !empty($_POST['password'])) {
$psw=mysqli_real_escape_string($conn,trim($_POST['password']));
}else{
$empty_password="Password cannot be empty";
echo $empty_password.'<br>';
}

if (isset($_POST['re_password']) && !empty($_POST['re_password'])) {
$repsw=mysqli_real_escape_string($conn,trim($_POST['re_password']));
}else{
$empty_repassword="Retype password cannot be empty";
echo $empty_repassword.'<br>';
}

$password=password_hash('$psw',PASSWORD_BCRYPT);
$date=mysqli_real_escape_string($conn, trim('now()'));
if($psw!=$repsw)
{
echo"password not Matched";
}
$sql="select * from account_info where (username='$username' or email='$email');";
$res=mysqli_query($conn,$sql);
if (mysqli_num_rows($res) > 0) {
// output data of each row
$row = mysqli_fetch_assoc($res);
if ($username==$row['username'])
{
echo "Username already exists";
}
elseif($email==$row['email'])
{
echo "Email already exists";
}
else{
echo "alright";
}
}
}
?>

最佳答案

你只需要在正确的地方添加else条件
试试这个:-

<?php
error_reporting(E_ALL & ~E_NOTICE);
require_once('dbcon.php');
if(isset($_POST["create"]))
{
$error = 0;
if (isset($_POST['username']) && !empty($_POST['username'])) {
$username=mysqli_real_escape_string($conn,trim($_POST['username']));
}else{
$error = 1;
$empty_username="Username Cannot be empty.";
echo $empty_username.'<br>';
}
if (isset($_POST['email']) && !empty($_POST['email'])) {
$email=mysqli_real_escape_string($conn,trim($_POST['email']));
}else{
$error =1;
$empty_email="Email cannot be empty.";
echo $empty_email.'<br>';
}

if (isset($_POST['category']) && !empty($_POST['category'])) {
$category=mysqli_real_escape_string($conn,trim($_POST['category']));
}else{
$error = 1;
$empty_category="Category cannot be empty.";
echo $empty_category.'<br>';
}

if (isset($_POST['password']) && !empty($_POST['password'])) {
$psw=mysqli_real_escape_string($conn,trim($_POST['password']));
}else{
$error = 1;
$empty_password="Password cannot be empty";
echo $empty_password.'<br>';
}

if (isset($_POST['re_password']) && !empty($_POST['re_password'])) {
$repsw=mysqli_real_escape_string($conn,trim($_POST['re_password']));
}else{
$error = 1;
$empty_repassword="Retype password cannot be empty";
echo $empty_repassword.'<br>';
}

$password=password_hash('$psw',PASSWORD_BCRYPT);
$date=mysqli_real_escape_string($conn, trim('now()'));
if($psw!=$repsw)
{
echo "password not Matched";
}

if(!$error) {
$sql="select * from account_info where (username='$username' or email='$email');";
$res=mysqli_query($conn,$sql);
if (mysqli_num_rows($res) > 0) {
// output data of each row
$row = mysqli_fetch_assoc($res);
if ($username==$row['username'])
{
echo "Username already exists";
}
elseif($email==$row['email'])
{
echo "Email already exists";
}
}else { //here you need to add else condition
echo "alright";
}
}
}
?>

关于php - 检查用户名和电子邮件已经存在于php mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42460896/

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