View Single Post
Old 04/02/2014, 08:51 PM   #30
PowerPC603
High-roller
 
Join Date: Nov 2010
Location: Belgium
Posts: 1,734
Reputation: 238
Default Re: MySQL Registration System [Threaded Queries(R33+) + Whirlpool]

pawn Code:
case 2: //register dialog
        {
            if(!response) return Kick(playerid); //if they clicked Quit, we will kick them
            if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.\nYour password must be at least 6 characters long!", "Register", "Quit");
            //strlen checks a lenght of a string. so if player types their password that is lower than 6, we tell them; Your password must be at least 6 characters long!
            WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
            mysql_format(mysql, query, sizeof(query), "INSERT INTO `players` (`Username`, `Password`, `IP`, `Admin`, `VIP`, `Money`, `posX` ,`posY`, `posZ`) VALUES ('%e', '%s', '%s', 0, 0, 0, 0.0, 0.0, 0.0)", Name[playerid], pInfo[playerid][Password], IP[playerid]);
            //Now let's create a new row and insert player's information in it
            mysql_tquery(mysql, query, "", "");
            //let's execute the query
        }
    }
When the player registers his account, the data is inserted properly.
BUT the tutorial doesn't have the ID of the player in the database.
Only the player's name, password and IP are inserted.
The table auto-creates the ID in MySQL, but it's not loaded and stays 0 in the script.

When exiting the game, OnPlayerDisconnect tries to update data for ID 0, which doesn't exist.

When the player logs back in (using the code from the tutorial), ALL data is read properly, including the ID.
Therefore it can be saved properly during OnPlayerDisconnect because now the script holds the proper ID of the player to update the data.




The code above should become (adjusted the query here):
pawn Code:
case 2: //register dialog
        {
            if(!response) return Kick(playerid); //if they clicked Quit, we will kick them
            if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.\nYour password must be at least 6 characters long!", "Register", "Quit");
            //strlen checks a lenght of a string. so if player types their password that is lower than 6, we tell them; Your password must be at least 6 characters long!
            WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
            mysql_format(mysql, query, sizeof(query), "INSERT INTO `players` (`Username`, `Password`, `IP`, `Admin`, `VIP`, `Money`, `posX` ,`posY`, `posZ`) VALUES ('%e', '%s', '%s', 0, 0, 0, 0.0, 0.0, 0.0)", Name[playerid], pInfo[playerid][Password], IP[playerid]);
            //Now let's create a new row and insert player's information in it
            mysql_tquery(mysql, query, "OnAccountCreate", "i", playerid);  // Adjusted the query
            //let's execute the query
        }
    }


Then use code like this:
pawn Code:
// This custom callback is used after creating a new account (playername and password are inserted, but the ID must be loaded now to reference the player later on using this ID)
forward OnAccountCreate(playerid);
public OnAccountCreate(playerid)
{
    // Setup local variables
    new Query[128];

    // Construct the query to get the new player's SQLID for later referencing and execute it
    mysql_format(SQL_db, Query, sizeof(Query), "SELECT ID FROM %s WHERE PlayerName = '%e' LIMIT 1", table_playerdata, APlayerData[playerid][PlayerName]);
    mysql_tquery(SQL_db, Query, "OnAccountCreateLoadID", "i", playerid);

    return 1;
}

// This custom callback is used after creating a new account and executing the query to load the new player's SQLID for later referencing
forward OnAccountCreateLoadID(playerid);
public OnAccountCreateLoadID(playerid)
{
    // Store the loaded ID
    APlayerData[playerid][SQLID] = cache_get_field_content_int(0, "ID", SQL_db);

    return 1;
}

Of course, adjust database names, variablenames and mysql-connectionhandle to the proper values in your script, as this was literally taken out of my script.
__________________
Gamemode: PPC_Trucking
Extras-file (for PPC_Trucking): /delproperty, /evict, /propertyid, /porthouse, /portbus, /properties
------------------------------------------------------------------------------------
PPC-Trucking-V2 (home-hosted, only open on occasion for testing purposes): ppctruckingv2.no-ip.info:7777
Youtube channel: https://www.youtube.com/channel/UC-4...Mpglma8f0Ld-YQ
------------------------------------------------------------------------------------
Filterscripts: PPC_Speedometer - PPC_Housing - PPC_Business
PowerPC603 is offline   Reply With Quote