开发者

Is is possible to do this with only one sql?

开发者 https://www.devze.com 2023-03-21 00:49 出处:网络
I want have an insert query, but before inserting I check whether the username and email are used by someone else. If used, I want to cancel insert query and echo a message to say whether username or

I want have an insert query, but before inserting I check whether the username and email are used by someone else. If used, I want to cancel insert query and echo a message to say whether username or email is in use.

Here my code:

$sql = "SELECT 1 FROM user WHERE username='".$_POST['username']."'";
if(!$result = mysql_query($sql))
    die(mysql_error());
while($row = mysql_fetch_array($result))
    die('This username is already exists');
$sql = "SELECT 2 FROM user WHERE email='".$_POST['email']."'";
if(!$result = mysql_query($sql))
    die(mysql_error());
while($row = mysql_fetch_array($result))
    die('This email address is already exists');
$sql = "insert into user (username,email,password,tel,type) values ('".$_POST['username']."','".$_POST['email']."','".$_POST['password']."','".$_POST['telnumber']."','member')";
if(!mysql_query($sql))
    die(mysql_error());

I wan开发者_JS百科t these three sql statements in one. It can be either using cases or something else that you suggest. So, Is it possible to zip this code into one sql query?

As a result what I need is

sql = "sql_query"
if(!$result = mysql_query($sql))
    die(mysql_error());
while($row = mysql_fetch_array($result)){
if($row['result']==1)
    die('This username is already exists');
else if($row['result']==2)
    die('This email is already exists');
}
die('you have succesfully registered');

thanks for any advice.


While I suggest you follow @cularis' answer, you may be interested in the following alternative:

  1. Give email and username the UNIQUE constraint, by creating a unique index for both of these.
  2. run your INSERT query, and if this fails... (due to duplicate keys)
  3. run the suggested combined SELECT, to determine which field existed (username or email)


You can combine the first two queries like this:

$sql = "SELECT * FROM user WHERE username='".$_POST['username']."' OR email='".$_POST['email']."'";

Have look at mysql_real_escape string to sanatize your input.


Assuming you don't care about a more specific error case you could probably just do the following:

$sql = "SELECT * FROM user WHERE username='".$_POST['username']."' OR email='".$_POST['email']."'";
if(!$result = mysql_query($sql))
    die(mysql_error());
while($row = mysql_fetch_array($result))
    die('The username or email address is already being used');
$sql = "insert into user (username,email,password,tel,type) values ('".$_POST['username']."','".$_POST['email']."','".$_POST['password']."','".$_POST['telnumber']."','member')";
if(!mysql_query($sql))
    die(mysql_error());

This isn't the best of designs if you're looking for, as I said, specific error cases. So if you are okay with just telling the person there is an error that one or both are in use then that should work.

I am not sure as I am very rusty in PHP/MySQL but I assume that if such cases of both exist then multiple rows may be returned and I forget exactly how mysql_fetch_array works but I assume it's an array of all results valid for the query so you should be set. As long as the array exists, you know there was a hit in the db.

0

精彩评论

暂无评论...
验证码 换一张
取 消