如何检查表中用户是否存在

问题描述 投票:0回答:2

如果

$name
$email
与现有记录不匹配,我正在尝试为新用户创建帐户,但以下代码片段正在插入与另一个客户端相同的记录,而我试图阻止这种情况:
if( $stmt->num_rows > 0 ) {}

<?php

include_once"dbconfig.php";
$name = "aaa";
$email = "[email protected]";
$phone = "666";
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$sql = "SELECT id, name, email,phone FROM `clients` WHERE `email` = '". $email."' AND `phone` = ". $phone;
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$stmt->bind_result($id, $name, $email, $phone);
$stmt->store_result();

if( $stmt->num_rows > 0 ) {
       echo "This Email already is in system";
    }else{
        $stmt = $mysqli->prepare("INSERT INTO clients (`name`,`email`,`phone`) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $email, $phone, $name);
        $stmt->execute();
        echo "New records created successfully";
    }
$stmt->close();
$mysqli->close();

?>

我做错了什么?

php mysqli prepared-statement
2个回答
0
投票

试试这个,

$sql = "SELECT id, name, email FROM `clients` WHERE `email` = '". $email."' AND `phone` = ". $phone;

-1
投票

我认为问题出在你的sql语句中,你检查记录是否存在,在:

“电话”>“。$电话

© www.soinside.com 2019 - 2024. All rights reserved.