douou0977 2017-03-10 19:24
浏览 221

确保用户名和电子邮件地址是唯一的

I am trying to verify whether or not a username and email address exist in my database and have tried to do a if, elseif, else statement to no avail.

I want to first run a check to see if the username is fine - obviously if not, an echo statement will appear. If the username doesn't exist, run an elseif statement to see if the email address is unique - again if not, another echo statement will appear. For the final statement, if all other conditions return false, I want to run the below code so that the user's input is submitted to the database.

I initially tried to declare two variables with a statement to check if the username=$username and email_address=$email_address then check to see if the number of rows returned from a mysqli_query is more than 1 for the username. I entered an elseif statement with the same but for email address but then I had an else statement with the below code in {} brackets.

I have deleted the original code because too many errors were thrown up, and was probably too convoluted and messy when a more elegant way to do what I was exists.

Any help would be appreciated.

if(isset($_POST['submit']))
    {
$sql = "INSERT INTO users (first_name, last_name, username, email_address, password,    gender, city, country, verification_code, verified, sign_up_date) VALUES (
'$first_name',
'$last_name',
'$username',
'$email_address',
'$password',
'$gender',
'$city',
'$country',
'$verification_code',
'1',
'$sign_up_date')";
    $result = mysqli_query($conn,$sql);
    header("Location:confirmation.php");
}   
  • 写回答

3条回答 默认 最新

  • doubleyou1001 2017-03-10 19:32
    关注

    What you want is an integrity check on the data. You should do this check inside the database. The simplest way is with unique constraints/indexes:

    create unique index unq_users_username on users(username);
    create unique index unq_users_email on users(email);
    

    If you attempt to insert or update a row so it violates these constraints, then your data modification step will fail with an error.

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来