我正在使用社交网络,如果用户名或电子邮件相同,我不想将详细信息输入MySQL数据库。
这是我正在使用的PHP:
if (isset($_POST['username']) && isset($_POST['password'])){
$datejoined = date('m/d/Y h:i:s a', time());;
$verified = 0;
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO `members` (username, password, email, verified, datejoined)
VALUES ('$username', md5('$password'), '$email', '0', '$datejoined')";
真的很感激一些帮助!
答案 0 :(得分:1)
创建唯一索引以发现重复项。
create unique index unq_members on members(username);
create unique index unq_members on members(email);
然后,在执行插入时忽略重复项:
INSERT IGNORE INTO `members`(username, password, email, verified, datejoined)
VALUES ('$username', md5('$password'), '$email', '0', '$datejoined');
或使用:
INSERT INTO `members`(username, password, email, verified, datejoined)
VALUES ('$username', md5('$password'), '$email', '0', '$datejoined')
ON DUPLICATE KEY UPDATE username = username;