mysql防止用户名重复,如何防止PHP / mysql中重复的用户名

(This is my first try with php)

* I just remembered PHP code doesn't show up in the source so here it is:

enter code here

include('connection.php');

if(isset($_POST['form'])){

if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['conf_pass']) || empty($_POST['email'])){

echo 'Please fill out all fields.';

}elseif($_POST['password'] != $_POST['conf_pass']){

echo 'Your Passwords do not match.';

}else{

$url = 'http://graves-incorporated.com/test_sites/plantation_park_2012/';

echo '';

echo 'Congrats, You are now Registered.';

mysql_query("INSERT INTO users VALUES(NULL, '$_POST[username]', '$_POST[password]', '$_POST[email]')");

}

}

?>

I want to make sure I don't get duplicate users and/or email addresses in the database. I set up a Unique Key in MySQL for Username and Email, which prevents it, but the user on the actual form doesn't know that, it still tells them "Congrats, you are signed up" or whatever it says... haha

So what can I add to the php code (and where in the code) that would prevent this?

Thanks for helping this major noob,

Dan Graves

解决方案<?php

include('connection.php');

if(isset($_POST['form'])){

if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['conf_pass']) || empty($_POST['email'])){

echo 'Please fill out all fields.';

}elseif($_POST['password'] != $_POST['conf_pass']){

echo 'Your Passwords do not match.';

}else{

$dup = mysql_query("SELECT username FROM users WHERE username='".$_POST['username']."'");

if(mysql_num_rows($dup) >0){

echo 'username Already Used.';

}

else{

$url = 'http://graves-incorporated.com/test_sites/plantation_park_2012/';

echo '';

$sql = mysql_query("INSERT INTO users VALUES(NULL, '$_POST[username]', '$_POST[password]', '$_POST[email]')");

if($sql){

echo 'Congrats, You are now Registered.';

}

else{

echo 'Error Registeration.';

}

}

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值