开发者

Prevent duplicates in database

$db->query("SELECT * FROM ".DB_PREFIX."users WHERE uid='".$uid_id."' AND login='ExpressCheckoutUser'");
                if ($db->moveNext())
                {

$db->assignStr("address1", $_REQUEST['address_street']);
$db->assignStr("city", $_REQUEST['address_city']);
$db->assignStr("state", $_REQUEST['address_state']);
$db->assignStr("fname", $_REQUEST['first_name']);
$db->assignStr("lname", $_REQUEST['last_name']);
$db->assignStr("email", $_REQUEST['payer_email']);

$db->assignStr("country", $country_code);
$db->assignStr("zip", $_REQUEST['address_zip']);
$db->update(DB_PREFIX."users", "WHERE uid='".$uid_id."'");
$db->reset();
}

everytime i make payment via paypal, my info will be captured in database but i wanted to prevent duplicates. so how do i go around it? Or should I check email duplicates?

EDIT As far as I can tell, uid is set to primary by pinnaclecart. so wouldnt it 开发者_C百科be 'dangerous' to set it to be unique instead?


First and last name are nice, but everything but unique. I know a few people that have the same name I do, so I guess building a unique index on those two columns will only frustrate, not help. The thing that makes me unique though is that I am the only one who has both my e-mail address and password, so I think that would be a better candidate.

ALTER TABLE users ADD UNIQUE unique_emailaddress ( email );

That should at least help with some of the duplicates, but not all: users may have multiple e-mail addresses (I know I do ;)), but it still better than an arbitrary combination of first and last name which isn't unique at all.


If all you need is a single UNIQUE column, you can do something like:

ALTER TABLE `users` ADD UNIQUE `lname`(fname);

If you set a column to UNIQUE it will only make that column unique, so if you have two people, one named "John Smith" and another named "Jane Smith", a UNIQUE on the lname will cause the second to fail. If you set UNIQUE keys on both first and last name fields separately, then you will fail in either case of first or last names being the same.

You will probably instead wish to add a compound key to enforce uniqueness across multiple fields combined. For this:

ALTER TABLE `users` ADD UNIQUE `unique_key`(fname,lname);

This would force a constraint in the database that would throw an error if you tried to create a duplicate record with the same first and last name.

You can throw exceptions on error, and handle these higher up in your codebase, or you can instead just see that you have an error and choose to ignore it.


Considering your last edit which says

uid is set to primary by pinnaclecart. so wouldnt it be 'dangerous' to set it to be unique instead?

In this case, don't do that. And don't do anything at all, PRIMARY KEY is UNIQUE by default, so it can not be duplicated.


Create a UNIQUE index in the database.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜