重复条目 mysql 和 php [英] duplicate entries mysql and php

查看:43
本文介绍了重复条目 mysql 和 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的数据库字段用户名"和电子邮件"设置为 unquie,当使用下面的代码时,这仅在用户名"已经存在时才有效,然后会回显错误.如果他们的电子邮件存在,则用户会收到 mysql 重复错误,此时应显示与上述相同的错误.

I have set my database fields "username" and "email" to unquie, when using the code below this only works if the "username" already exists, an error is then echoed. If they email exists the user gets a mysql duplicate error, when the same error as above should be shown.

<?php

require_once ( 'connection.php' );

$username=$_POST['username']; 
$password=md5($_POST['password']);
$email=($_POST['email']);
$ip=$_SERVER['REMOTE_ADDR'];
session_start();

$query = "INSERT INTO users (username, password, email, rank, ip, active) VALUES     ('$username','$password', '$email', '1', '$ip', '0')";

$sql = "SELECT username AND email FROM users WHERE username = '$username' AND  email     = '$email'" ;
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$row = mysql_fetch_array($result);

if ( $count== 0 )
{
if (!mysql_query($query))
{
 die('Error: ' . mysql_error());
}
  echo "You are signed up, please follow the link on your email to active your        account.";
}
else
 {
  echo "Username or Email already exists"."<br><a href=\"sign_up.php\">Try    Again</a></br>";
 }  
?

谢谢

推荐答案

尝试切换

WHERE username = '$username' AND  email     = '$email'"

WHERE username = '$username' OR email     = '$email'"

编辑:我想猜测您在这里要做什么.根据您的描述,您似乎希望用户名或电子邮件是唯一的,并且在这些列上有两个单独的唯一索引.您的代码检查用户名和电子邮件的组合是否唯一.

Edit: I'm trying to guess what you're trying to do here. From your description, it seems you want either the username or the email to be unique and you have two separate unique indexes on those columns. Your code checks for the combination of username and email to be unique.

编辑 2:另外,我认为您可能想了解 SQL 注入并发.

Edit 2: Also, I think you might want to look into the concepts of SQL Injection and Concurrency.

这篇关于重复条目 mysql 和 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆