Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-15-2011, 10:35 AM Checking Duplicates
Skilled Talker

Posts: 87
Trades: 0
Sorry, this might be a newbie question, but here it is:

I want to check user input for duplicates already in the database. For example, if the user inputs "foobar" and "foobar" is already in the database, I want to return an error.

This is easy.

I also want to return an error if the user types in "fooBar" or "foobar " or "FoObAr" or "Foo Bar." How do I check that with a MySQL query?

Thanks so much in advance.
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
 
Register now for full access!
Old 06-15-2011, 10:46 AM Re: Checking Duplicates
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
do you have a php script to handle the form yet?? if you you do can you post it?? it'll be easier to help..
stivens is offline
Reply With Quote
View Public Profile
 
Old 06-15-2011, 11:10 AM Re: Checking Duplicates
Skilled Talker

Posts: 87
Trades: 0
It's everywhere... but, here are some pertinent parts:

Code:
$name = MySQL::SecureData(isFilledOut(checkDuplicateName($_POST['name']), 'program', $college_id));
Code:
function checkDuplicateName ($name, $type, $upperID = 0, $user = 0) {
global $db;
	switch ($type) {
		case 'program':
			$db->ExecuteSQL("SELECT * FROM cl_user_programs WHERE programName = '$name' AND collegeID = '$upperID'");
			if ($db->getNumRecordsReturned != 0)
				error('A program with that name is already associated with this college! Please try again with a different name.');
			break;
		case 'scholarship':
			$db->ExecuteSQL("SELECT * FROM cl_user_scholarships WHERE scholarshipName = '$name' AND collegeID = '$upperID'");
			if ($db->getNumRecordsReturned != 0)
				error('A scholarship with that name is already associated with this college! Please try again with a different name.');
			break;
		case 'college':
			$db->ExecuteSQL("SELECT * FROM cl_user_colleges WHERE collegeName = '$name' AND user = '$user'");
			if ($db->getNumRecordsReturned != 0)
				error('A college with that name is already in your list of colleges! Please try again with a different name.');
		default:
			error();
}
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 06-15-2011, 01:46 PM Re: Checking Duplicates
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
This should work, but I didn't test it. The MySQL REPLACE() will replce spaces with empty string and using the LIKE compare will make it case insensitive.


PHP Code:
function checkDuplicateName ($name$type$upperID 0$user 0) {
global 
$db;
 switch (
$type) {
  case 
'program':
   
$db->ExecuteSQL("SELECT * FROM cl_user_programs WHERE collegeID = '$upperID' HAVING REPLACE(programName, ' ', '') LIKE REPLACE('$name', ' ', '')");
   if (
$db->getNumRecordsReturned != 0)
    
error('A program with that name is already associated with this college! Please try again with a different name.');
   break;
  case 
'scholarship':
   
$db->ExecuteSQL("SELECT * FROM cl_user_scholarships WHERE collegeID = '$upperID' HAVING REPLACE(scholarshipName, ' ', '') LIKE REPLACE('$name', ' ', '')");
   if (
$db->getNumRecordsReturned != 0)
    
error('A scholarship with that name is already associated with this college! Please try again with a different name.');
   break;
  case 
'college':
   
$db->ExecuteSQL("SELECT * FROM cl_user_colleges WHERE user = '$user' HAVING REPLACE(collegeName, ' ', '') LIKE REPLACE('$name', ' ', '')");
   if (
$db->getNumRecordsReturned != 0)
    
error('A college with that name is already in your list of colleges! Please try again with a different name.');
  default:
   
error();

__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-15-2011, 02:20 PM Re: Checking Duplicates
Skilled Talker

Posts: 87
Trades: 0
Will definitely check out this solution. Thanks so much!
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 06-16-2011, 12:39 AM Re: Checking Duplicates
prasanthmj's Avatar
Experienced Talker

Posts: 42
Name: Prasanth
Trades: 0
yuechen,
After looking at your code, it seems you will be interested in Full text search feature
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
prasanthmj is offline
Reply With Quote
View Public Profile
 
Old 06-16-2011, 04:08 PM Re: Checking Duplicates
Skilled Talker

Posts: 87
Trades: 0
@prasanthmj, It looks interesting. Thanks for the heads up!

@mgraphic, THANK YOU! I'm still a newbie to SQL commands and it looks like I have HAVING and REPLACE under my belt! Your solution worked beautifully.
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 06-17-2011, 09:02 AM Re: Checking Duplicates
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
I'd have thought that "select count(*) " would be faster than "select *" .
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 06-17-2011, 09:32 AM Re: Checking Duplicates
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by PaulW View Post
I'd have thought that "select count(*) " would be faster than "select *" .
I don't know for sure, but I know with "SELECT COUNT(*)" would still require you to return the result in an array because it would always return a row (and you could not use mysql_num_rows). One thing I would do is select only the primary id field rather than all (*) fields to reduce the amount of memory used to do a simple count.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-17-2011, 12:38 PM Re: Checking Duplicates
Skilled Talker

Posts: 87
Trades: 0
That's an excellent suggestion! Haha I didn't pay much attention to anything besides workability in this prototype code, so memory overusages are everywhere. I shall look into SELECT COUNT as I've never heard of it before. Thanks again!
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 06-18-2011, 11:38 AM Re: Checking Duplicates
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Does MySQL have the equivalent of Oracle's SELECT COUNT(1) ?

And yes, select count(*) is bad - grounds for dismissal if you're working for me - I didn't want to complicate the initial answer.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Checking Duplicates
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.67933 seconds with 12 queries