如果$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();
?>
你能让我知道我在做什么错吗?
仅供参考,您的代码很容易出现SQL注入 –