Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1957 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

PHP Help needed

Thread title: PHP Help needed
Closed Thread    
    Thread tools Search this thread Display Modes  
08-23-2006, 07:53 PM
#1
Jeff Andersen is offline Jeff Andersen
Status: Superstar
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 4,449
iTrader: 10 / 100%
 

Jeff Andersen is on a distinguished road

  Old  PHP Help needed

I'm making a category addition onto my news script, but i need to (in the add category section) have the script tell the person to choose another name if the name was taken..

How would you query that?

08-23-2006, 08:03 PM
#2
iisbum is offline iisbum
iisbum's Avatar
Status: Member
Join date: Oct 2005
Location: Clifton Park, NY, USA
Expertise:
Software:
 
Posts: 273
iTrader: 4 / 100%
 

iisbum is on a distinguished road

  Old

You can do this:

PHP Code:
select count(*) as count from your_table where your_name_field '$nameVariable' 
If the count is greater than 0, then the name is already taken.

HTH,
Mubs

08-23-2006, 10:38 PM
#3
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

^ That is probably the most effective way to do it.

Then just do, if($mysql_query > 0) { echo "Name taken..."; } else { // Commands }.

08-23-2006, 11:27 PM
#4
Jeff Andersen is offline Jeff Andersen
Status: Superstar
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 4,449
iTrader: 10 / 100%
 

Jeff Andersen is on a distinguished road

  Old

Hmm i just tried that and when i submit anything now it gives me the "This has been submitted already" thing :S.

Regards,

edit:

Doing this right?

PHP Code:
$result=MYSQL_QUERY("select count(*) as count from cats where name = '".$_POST['name']."'");
if(
$result 0) { 
#### Header
include('templates/header.htm');
print 
"<h3>Your category name has already been taken, please <a href=\"javascript:history.back();\">select another</a>.</h3><br>";
}
else
{
blah

08-24-2006, 12:07 AM
#5
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old

you want to prevent someone from creating a category i the name already exists right?

i'd make something that follows this logic:

PHP Code:
/**
  * store the $_POST[] array in seperate variables so they're easier to work with.
  * use trim() to strip away any white space. i'd also use strtolower() and ucword to
  * ensure people don't use horrid casing like DOwnlAdS.
**/
$name ucwords(strtolower(trim($_POST['name'])));
/**
   * the query is going to look for an exact match compared to what was
   * through the form. the reason i suggest the ucwords() & strtolower()
   * is because every title will follow the same naming convention and you
   * won't run into comparing Downloads to DoWNLoADs and failing your
   * cuplication check
**/
$query "SELECT * from cats WHERE name = '$name'";
$result mysql_query($query);

// check to see if we have a duplicate entry
if(count($result 0) {
    require_once(
"templates/header.html");
    echo 
"<h3>Your category name has already been taken, please <a href=\"javascript:history.back();\">select another</a>.</h3><br>";
    exit();
} else {
    ** 
any other db queries go hereIE adding the entry

08-24-2006, 12:26 AM
#6
Jeff Andersen is offline Jeff Andersen
Status: Superstar
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 4,449
iTrader: 10 / 100%
 

Jeff Andersen is on a distinguished road

  Old

I'm still getting the error on all submissions

08-24-2006, 10:53 AM
#7
KarlP is offline KarlP
KarlP's Avatar
Status: I'm new around here
Join date: Apr 2006
Location:
Expertise:
Software:
 
Posts: 24
iTrader: 0 / 0%
 

KarlP is on a distinguished road

  Old

Originally Posted by Jeff Andersen
I'm still getting the error on all submissions

Keep the same query, but try this:

if (($result) && (mysql_num_rows($result) > 0))
{
// Duplicate
}
else
{
// No duplicate
}

08-24-2006, 11:18 AM
#8
Jeff Andersen is offline Jeff Andersen
Status: Superstar
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 4,449
iTrader: 10 / 100%
 

Jeff Andersen is on a distinguished road

  Old

Wow great, that worked. Thanks a billion Karl and the rest of the group who help!

Regards,

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed