RE: [PHP] How to prevent duplicated values?

2004-01-28 Thread SLanger
Hello

I think using UNIQUE is the way to go simply because of data integrety (= 
how do you spell this??? ; ) ) If you don't use UNIQUE and you are not 
write locking your table you might accidently get duplicate Values. UNIQUE 
is the only way to prevent duplicate data without a lot of hassle. This 
doesn't mean you cannot check upon duplicates first, which I think is 
obsolete since you need to add error checking code anyway.

Regards 
Stefan Langer 


RE: [PHP] How to prevent duplicated values?

2004-01-27 Thread Ralph Guzman

What I would do would instead be to put an 
index on that column, then with a simple select 
check if that username is free or not. Based on 
what I get in return I can then either insert the 
data or return the form to the visitor with information 
about it. Much more simple in the long run.

I don't see how this method is simpler than querying the table to see if
user name exists then returning true or false. Either way you are inserting
the data or returning and error to be displayed on the form.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How to prevent duplicated values?

2004-01-26 Thread BAO RuiXian


Radwan Aladdin wrote:

Hello..

I want to prevent mySQL from storing the same value in a specified field (Username).. so how can I do it?

Regards..
 

Make it field UNIQUE.

Best

Bao

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] How to prevent duplicated values?

2004-01-26 Thread Mario
Run a select first to check whether you have that value stored and by using
an if statement add the new value or show an error.
- Original Message - 
From: BAO RuiXian [EMAIL PROTECTED]
To: Radwan Aladdin [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Sent: Monday, January 26, 2004 11:35 AM
Subject: Re: [PHP] How to prevent duplicated values?




 Radwan Aladdin wrote:

 Hello..
 
 I want to prevent mySQL from storing the same value in a specified field
(Username).. so how can I do it?
 
 Regards..
 
 
 Make it field UNIQUE.

 Best

 Bao

 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] How to prevent duplicated values?

2004-01-26 Thread Thomas Svenson
BAO RuiXian wrote:
 Radwan Aladdin wrote:
 I want to prevent mySQL from storing the same value in a
 specified field (Username).. so how can I do it?

 Make it field UNIQUE.

That's the easy method, but usually not very smart. It only causes MySQL to
generate an error which you then have to take care of in your application.

Much better to have an index on the Username column and then use a select to
check if the name exist. If it doesn't it is OK to insert. Much easier to
handle in PHP.

Personally I avoid using such things as UNIQUE or NOT_NULL (unless the
column have a default value) on columns, just because it is more complex to
handle the MySQL errors than write your own code to check it in PHP.

/T

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] How to prevent duplicated values?

2004-01-26 Thread Thomas Svenson
Radwan Aladdin wrote:
 But I'm using VB OCX file to show the result... I tried the UNIQUE
 and it worked greate! So do you mean I should use other ways?

If it works, then great.

The problem, as I can see it, is if you have a form where a user applies for
a user account (I assume that since the column name is Username) and you
then query MySQL to add the data to the table.

If that user name already exist, MySQL will give an error saying that it
can't insert since there is already a row with that name. You will have to
handle this and then tell the new user that the name he wants already is
taken.

What I would do would instead be to put an index on that column, then with a
simple select check if that username is free or not. Based on what I get in
return I can then either insert the data or return the form to the visitor
with information about it. Much more simple in the long run.

Also this will put you in charge of fault handling and not the database.
Remember that PHP just send/receive data to/from MySQL. It can't interpret
error messages MySQL outputs. Therefore I prefer to check things like this
myself.

It is pretty easy to write global functions that then can be used in many
places for this instead of handling the error messages from MySQL.

/T

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How to prevent duplicated values?

2004-01-26 Thread BAO RuiXian


Thomas Svenson wrote:

BAO RuiXian wrote:
 

Make it field UNIQUE.
   

That's the easy method, but usually not very smart. It only causes MySQL to
 

Well, if there is an easy method, why we bother to use a more 
complicacted way?

generate an error which you then have to take care of in your application.
 

We need to take care of various errors any way whenever we are dealing 
with databases.

Much better to have an index on the Username column and then use a select to
check if the name exist. If it doesn't it is OK to insert. Much easier to
handle in PHP.
 

Quite to the opposite, if the database can handle it, why bother to 
write one's own with something adding more burden to the database 
(select statement), more process on the php code (check each item of the 
select result) and possibly less error-proof in your php checking code?

Personally I avoid using such things as UNIQUE or NOT_NULL (unless the
column have a default value) on columns, just because it is more complex to
handle the MySQL errors than write your own code to check it in PHP.
 

Can't get it. If there is a existing method, why not use it. The 
home-brew one is always low in aspects. I don't understand why you think 
checking errors is complext, which has to be done in all your codes, 
even a trivial one.

/T
 

Best

Bao

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php