Curses... Unique naming

Forum
Last Post
Threads / Messages

SilverDragonTears

I am your Nemesis.
Premium Member
Joined
Jun 1, 2011
Messages
1,113
Points
36
Mysidian Dollar
51,802
When a user names their pet and the name is already taken, how can I show that? I have it set where names are unique.

Code:
		// We are renaming an adoptable

			// Now we see if the adoptable actually exists...

			$stmt = $adopts->query("SELECT * FROM {$prefix}owned_adoptables WHERE owner='{$loggedinname}' and aid='{$id}'");
			$row = $stmt->fetchObject();

			if($row->aid == $id)
			{
				$image = getcurrentimage($id);

					if($more == "")
					{

						$article_title = "Rename {$row->name}";
						$article_content = "<img src='{$image}'><br />{$lang_rename}{$row->name}{$lang_rename2}<br />
											<form name='form1' method='get' action='myadopts.php'>
												<p>Adoptable Name: 
													<input name='more' type='text' id='more'>
													<input name='id' type='hidden' id='id' value='{$id}'>
													<input name='act' type='hidden' id='act' value='rename'>
												</p>
												<p>
													<input type='submit' name='Submit' value='Rename Adoptable'>
												</p>
											</form>";
					}

					else
					{
						// We are renaming the adoptable
 
Well it isnt really difficult, just find this part in your script:

PHP:
             else{
                        // We are renaming the adoptable

                        // The adoptable exists, so now we can rename it...
                        $adopts->query("UPDATE {$prefix}owned_adoptables SET name='{$more}' WHERE aid='{$id}' and owner='{$loggedinname}'");

                        $article_title = $lang_rename_success_title;
                        $article_content = "<img src='{$image}'><br />{$lang_rename_success}{$more}. 
                                            You can now manage {$more} on the <a href='myadopts.php?act=manage&id={$id}'>My Adopts</a> page";
                    }
And replace with:

PHP:
             else{
                    $stmt = $adopts->query("SELECT * FROM {$prefix}owned_adoptables WHERE name='{$more}' and aid='{$id}'");
                    $row = $stmt->fetchObject();
                    if(!is_object($row)){ 
                       // The name has not yet been used, we are good to go!
                        $adopts->query("UPDATE  {$prefix}owned_adoptables SET name='{$more}' WHERE aid='{$id}' and  owner='{$loggedinname}'");

                        $article_title = $lang_rename_success_title;
                        $article_content = "<img src='{$image}'><br />{$lang_rename_success}{$more}. 
                                            You can now manage {$more}  on the <a href='myadopts.php?act=manage&id={$id}'>My  Adopts</a> page";
                     }
                     else{
                      // The name is being used by someone else, so display an error message...
                        $article_title = "An error has occurred";
                        $article_content = "It appears that the name {$more} is taken already.";
                     }
                  }
 
Assuming you have a unique index on the name field, this can be achieved without any additional query.

PHP:
$stmt = $adopts->query('...');
if ( ! $stmt)
{
    // failed, the name is already in use
}
else
{
    // successfully renamed
}
I think this would be a much better solution, because not only that you keep the MySQL from choking, but you also keep your code succinct.
 
Last edited:
Good idea Fadillzzz, $stmt should return false if the query fails. This should significantly reduce the memory consumption for cases that the script merely checks if a row exists. Also it is better just to select the field name from the database instead of using '*', I just showed her an example of how to do this kind of trick though.
 
Pardon me for being ignorant... where do I put this and what do I put in the ('...')

Assuming you have a unique index on the name field, this can be achieved without any additional query.

PHP:
$stmt = $adopts->query('...');
if ( ! $stmt)
{
    // failed, the name is already in use
}
else
{
    // successfully renamed
}
I think this would be a much better solution, because not only that you keep the MySQL from choking, but you also keep your code succinct.
 
Oh my I must suffered from a lack of sleep then, lol. Remove this part from the very first line and it should work:

PHP:
 and aid='{$id}'
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,122
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Top