Mysidia Adoptables Support Forum

Mysidia Adoptables Support Forum (http://www.mysidiaadoptables.com/forum/index.php)
-   Questions and Supports (http://www.mysidiaadoptables.com/forum/forumdisplay.php?f=18)
-   -   Curses... Unique naming (http://www.mysidiaadoptables.com/forum/showthread.php?t=3538)

SilverDragonTears 03-27-2012 01:52 AM

Curses... Unique naming
 
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


Hall of Famer 03-27-2012 02:53 AM

Well it isnt really difficult, just find this part in your script:

PHP Code:

             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 Code:

             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.";
                     }
                  } 


fadillzzz 03-27-2012 03:13 AM

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

PHP Code:

$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.

Hall of Famer 03-27-2012 03:16 AM

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.

SilverDragonTears 03-27-2012 04:09 PM

Hof yours didn't work :/

SilverDragonTears 03-27-2012 04:47 PM

Pardon me for being ignorant... where do I put this and what do I put in the ('...')

Quote:

Originally Posted by fadillzzz (Post 22319)
Assuming you have a unique index on the name field, this can be achieved without any additional query.

PHP Code:

$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.


Hall of Famer 03-27-2012 04:48 PM

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 Code:

 and aid='{$id}' 


SilverDragonTears 03-27-2012 06:01 PM

What would I do without you Hof?

Hall of Famer 03-27-2012 06:04 PM

lol you flattered me. And I apologize for giving you the wrong codes in the first place. XD


All times are GMT -5. The time now is 04:35 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.