Jump to content

[SOLVED] checking if user already exists before adding them


Recommended Posts

hey people!

 

err it will probably look very very stupid to anyone that knows SQL but yea here is my failed attempt:

 INSERT INTO `users`
IF NOT EXIST(
`loginName` ,
`createDate` ,
`password` ,
`email`
)
VALUES (
'Kelz', '11.12.07', MD5( 'hello' ) , '', '', 'lala@yahoo.com'
) 

 

I was trying for ages and searching google but to be honest I'm not really sure what I'm looking for  ::) how do I insert this data if it's not already there?  ???

You definitely don't want to do it like that. There is no way to guarantee that every single person who will register has a different password and the createDate I am assuming it's the Day they register, what if two people register on the same Day?

 

Here's an excerpt for a sign in file I made a while ago...

 

if (empty($_POST['mem_dname']))
{
    die('You didn\'t enter a Display name.');
}
else
{
    $mem_dname = mysql_real_escape_string($_POST['mem_dname']);
}

$query = mysql_query("SELECT * FROM `gs_mem` WHERE `mem_dname` = '".$mem_dname."'");
$fetch = mysql_fetch_object($query);
if($fetch->mem_dname == $mem_dname)
{
    die('The Display name you have chosen is already in use.');
}

$query = mysql_query("SELECT * FROM `users`") or die(mysql_error());
while( $row = mysql_fetch_array($query))
{
$loginName = $row['loginName'];
$email = $row['email'];

if ("Kelz" == $loginName || "lala@yahoo.com" == $email)

/* 
You can add: md5('hello') == $password || $createDate = "11.12.07" but think about it,
if 2 users have registered on the same date and with the same password,
you don't want it to block 1 of em because he chose the same password or registered on the same date the other one did. 
if you still want it to be like this, also add $password = $row['password']; and $createDate = $row['createDate'];
*/ 

echo "loginName::Kelz already exists OR email::lala@yahoo.com is already in use.";


/* **** OR : ****
// (same thing, just look it it)

if("Kelz" == $loginName) echo "loginName:: Kelz already exists";
if("lala@yahoo.com == $email) echo "email::lala@yahoo.com is already in use.";

else
{
// Insert values...

mysql_query("INSERT INTO `users` (`loginName`, `createDate`, `password`, `email`) VALUES ('Kelz', '11.12.07', md5('hello'), 'lala@yahoo.com'"));

// BTW, your query was wrong -> after md5('hello') you typed ' ' and then ' ' again which is wrong - SQL thinks it's another value.
// Look at the query I typed, this one works.
}

}

 

If you seem that the code I typed is really long - it's not, I just typed a lot of comments to explain you things, lol.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.