用户名值存在时的PHP用户名列

时间:2013-07-06 21:02:38

标签: php mysql

当用户名列的用户名存在时,未插入

if(filter_var($_POST['username'], FILTER_VALIDATE_EMAIL)) {
// EMAIL OK
echo "yes";
} else {
// EMAIL INVALID
die('wrong');
}


$sql="insert into persons(username, password, firstname, lastname, country, mobile) VALUES ('$_POST[username]','$_POST[password]','$_POST[firstname]','$_POST[lastname]','$_POST[country]','$_POST[mobile]')";

4 个答案:

答案 0 :(得分:1)

if(filter_var($_POST['username'], FILTER_VALIDATE_EMAIL)) {
// EMAIL OK
// Query to database to check this username is unique after that insert your query
if (empty($databaseUser)) {
$sql="insert into persons(username, password, firstname, lastname, country, mobile) VALUES ('$_POST[username]','$_POST[password]','$_POST[firstname]','$_POST[lastname]','$_POST[country]','$_POST[mobile]')";
} else {
// This user exist
}
} else {
// EMAIL INVALID
die('wrong');
}

答案 1 :(得分:1)

在查询之前添加它,它应该没问题:

$result = mysqli_query($db,'SELECT * FROM persons WHERE username=\''.$_POST['username'].'\'');
if($result===false) die('Already registered.');

在这里,我假设您的数据库连接变量名为$ db

答案 2 :(得分:1)

if(isset($_POST['submit'])){
$user_username = trim($_POST['username']);
$user_password = trim($_POST['password']);
if(!empty($user_username) && !empty($user_password)){
$query="SELECT user_id,username from mismatch_user where username='$user_username' AND password = SHA('$user_password')";
$res=mysql_query($query);
if (!$res) die(mysql_error());
if(mysql_num_rows($res)==1){
echo 'already registered';}
}
}

答案 3 :(得分:0)

这将工作

$username = $_POST['username'];
$query = mysqli_query("SELECT * FROM persons WHERE username=$username");

if (isset($query)) {
die ("You Are Registred");
} else {
$inserting = mysql_query("insert into persons(username, password, firstname, lastname, country, mobile) VALUES ('$_POST[username]','$_POST[password]','$_POST[firstname]','$_POST[lastname]','$_POST[country]','$_POST[mobile]')");
   if (isset($inserting))
     echo "Done!!";
}

<强>更新

错误在这里 die ("You Are Registred")

通过添加(;)

来修复

die ("You Are Registred");

更新2

替换此

$inserting = mysql_query("insert into persons(username, password, firstname, lastname, country, mobile) VALUES ('$_POST[username]','$_POST[password]','$_POST[firstname]','$_POST[lastname]','$_POST[country]','$_POST[mobile]')")

用这个

$inserting = mysql_query("insert into persons(username, password, firstname, lastname, country, mobile) VALUES ('$_POST[username]','$_POST[password]','$_POST[firstname]','$_POST[lastname]','$_POST[country]','$_POST[mobile]')");