Adding new tables in SQL
#1

Hi! I'm following this MySQL Login / Registration tutorial made by Overhaul https://sampforum.blast.hk/showthread.php?tid=574714

my problem is if i add new table in mysql, and then when i try to login to the game, my character freezes. Thanks!
Reply
#2

Bump?
Reply
#3

You'll have to provide some code. Especially the code you added.
Reply
#4

We need some codes!
Reply
#5

PHP код:
#include <a_samp>
#include <a_mysql>
#define MYSQL_HOSTNAME "127.0.0.1"
#define MYSQL_USERNAME "root"
#define MYSQL_PASSWORD ""
#define MYSQL_DATABASE "samp"
#define START_X 2492.0830
#define START_Y -1665.6895
#define START_Z 13.3438
#define START_A 93.1757
new mysql;
native WP_Hash(buffer[], len, const str[]);
enum
{
    
DIALOG_LOGIN,
    
DIALOG_REGISTER
};
enum AccountData
{
    
ID,
    
Name[MAX_PLAYER_NAME],
    
Password[129],
    
IP[16]
};
new 
Account[MAX_PLAYERS][AccountData];
enum PlayerData
{
    
ID,
    
Name[MAX_PLAYER_NAME],
    
Admin,
    
VIP,
    
Money,
    
Float:PosX,
    
Float:PosY,
    
Float:PosZ,
    
Float:PosA
};
new 
Player[MAX_PLAYERS][PlayerData];
main()
{
}
public 
OnGameModeInit()
{
    
mysql_log(LOG_ALL);
    
mysql mysql_connect(MYSQL_HOSTNAMEMYSQL_USERNAMEMYSQL_DATABASEMYSQL_PASSWORD);
    
    if(
mysql_errno() != 0)
    {
        
printf("[MySQL] The connection has failed.");
    }
    else
    {
        
printf("[MySQL] The connection was successful.");
    }
    return 
true;
}
public 
OnGameModeExit()
{
    return 
true;
}
public 
OnPlayerRequestClass(playeridclassid)
{
    return 
true;
}
public 
OnPlayerConnect(playerid)
{
    
TogglePlayerSpectating(playeridtrue);
    
    new 
query[128], playername[MAX_PLAYER_NAME];
    
    
GetPlayerName(playeridplayernamesizeof(playername));
    
    
mysql_format(mysqlquerysizeof(query), "SELECT `Password`, `ID` FROM `account_data` WHERE `Name` = '%e' LIMIT 1"playername);
    
mysql_tquery(mysqlquery"OnAccountCheck""i"playerid);
    return 
true;
}
public 
OnPlayerDisconnect(playeridreason)
{
    new 
query[128], Float:pos[4];
    
GetPlayerPos(playeridpos[0], pos[1], pos[2]);
    
GetPlayerFacingAngle(playeridpos[3]);
    
mysql_format(mysqlquerysizeof(query), "UPDATE `player_data` SET `Admin` = %d, `VIP` = %d, `Money` = %d, `PosX` = %f, `PosY` = %f, `PosZ` = %f, `PosA` = %f WHERE `ID` = %d",
    
Player[playerid][Admin], Player[playerid][VIP], GetPlayerMoney(playerid), pos[0], pos[1], pos[2], pos[3], Player[playerid][ID]);
    
mysql_tquery(mysqlquery"""");
    return 
true;
}
public 
OnPlayerSpawn(playerid)
{
    return 
true;
}
public 
OnPlayerDeath(playeridkilleridreason)
{
    return 
true;
}
public 
OnVehicleSpawn(vehicleid)
{
    return 
true;
}
public 
OnVehicleDeath(vehicleidkillerid)
{
    return 
true;
}
public 
OnPlayerText(playeridtext[])
{
    return 
true;
}
public 
OnPlayerCommandText(playeridcmdtext[])
{
    return 
false;
}
public 
OnPlayerEnterVehicle(playeridvehicleidispassenger)
{
    return 
true;
}
public 
OnPlayerExitVehicle(playeridvehicleid)
{
    return 
true;
}
public 
OnPlayerStateChange(playeridnewstateoldstate)
{
    return 
true;
}
public 
OnPlayerEnterCheckpoint(playerid)
{
    return 
true;
}
public 
OnPlayerLeaveCheckpoint(playerid)
{
    return 
true;
}
public 
OnPlayerEnterRaceCheckpoint(playerid)
{
    return 
true;
}
public 
OnPlayerLeaveRaceCheckpoint(playerid)
{
    return 
true;
}
public 
OnRconCommand(cmd[])
{
    return 
true;
}
public 
OnPlayerRequestSpawn(playerid)
{
    return 
true;
}
public 
OnObjectMoved(objectid)
{
    return 
true;
}
public 
OnPlayerObjectMoved(playeridobjectid)
{
    return 
true;
}
public 
OnPlayerPickUpPickup(playeridpickupid)
{
    return 
true;
}
public 
OnVehicleMod(playeridvehicleidcomponentid)
{
    return 
true;
}
public 
OnVehiclePaintjob(playeridvehicleidpaintjobid)
{
    return 
true;
}
public 
OnVehicleRespray(playeridvehicleidcolor1color2)
{
    return 
true;
}
public 
OnPlayerSelectedMenuRow(playeridrow)
{
    return 
true;
}
public 
OnPlayerExitedMenu(playerid)
{
    return 
true;
}
public 
OnPlayerInteriorChange(playeridnewinterioridoldinteriorid)
{
    return 
true;
}
public 
OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    return 
true;
}
public 
OnRconLoginAttempt(ip[], password[], success)
{
    return 
true;
}
public 
OnPlayerUpdate(playerid)
{
    return 
true;
}
public 
OnPlayerStreamIn(playeridforplayerid)
{
    return 
true;
}
public 
OnPlayerStreamOut(playeridforplayerid)
{
    return 
true;
}
public 
OnVehicleStreamIn(vehicleidforplayerid)
{
    return 
true;
}
public 
OnVehicleStreamOut(vehicleidforplayerid)
{
    return 
true;
}
public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_LOGIN:
        {
            if(!
responseKick(playerid);
            new 
hashpass[129], query[128], playername[MAX_PLAYER_NAME];
            
GetPlayerName(playeridplayernamesizeof(playername));
            
WP_Hash(hashpasssizeof(hashpass), inputtext);
            
            if(!
strcmp(hashpassAccount[playerid][Password]))
            {
                
mysql_format(mysqlquerysizeof(query), "SELECT * FROM `player_data` WHERE `Name` = '%e' LIMIT 1"playername);
                
mysql_tquery(mysqlquery"OnPlayerLoad""i"playerid);
            }
            else
            {
                
SendClientMessage(playerid, -1"You have specified an incorrect password!");
                
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_PASSWORD"Login""Welcome player!\nYour account has been found in our database. Please fill in your password:""Login""Quit");
            }
        }
        case 
DIALOG_REGISTER:
        {
            if(!
response) return Kick(playerid);
            if(
strlen(inputtext) < 5)
            {
                
SendClientMessage(playerid, -1"Your password must at least contain more than 4 characters.");
                return 
ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_PASSWORD"Register""Welcome player!\nYour account has not been registered yet. Please fill in your desired password:""Register""Quit");
            }
            new 
query[128], query2[512], playername[MAX_PLAYER_NAME], playerip[16];
            
GetPlayerName(playeridplayernamesizeof(playername));
            
GetPlayerIp(playeridplayeripsizeof(playerip));
            
WP_Hash(Account[playerid][Password], 129inputtext);
            
            
mysql_format(mysqlquerysizeof(query), "INSERT INTO `account_data` (`Name`, `Password`, `IP`) VALUES ('%e', '%e', '%e')"playernameAccount[playerid][Password], playerip);
            
mysql_format(mysqlquery2sizeof(query), "INSERT INTO `player_data` (`Name`, `Admin`, `VIP`, `Money`, `PosX`, `PosY`, `PosZ`, `PosA`) VALUES ('%e', 0, 0, 0, %f, %f, %f, %f)"playernameSTART_XSTART_YSTART_ZSTART_A);
            
mysql_tquery(mysqlquery"OnAccountRegister""i"playerid);
            
mysql_tquery(mysqlquery2"OnPlayerRegister""i"playerid);
        }
    }
    return 
true;
}
public 
OnPlayerClickPlayer(playeridclickedplayeridsource)
{
    return 
true;
}
forward OnAccountCheck(playerid);
public 
OnAccountCheck(playerid)
{
    new 
rowsfields;
    
    
cache_get_data(rowsfieldsmysql);
    if(
rows)
    {
        
cache_get_field_content(0"Password"Account[playerid][Password], mysql129);
        
Account[playerid][ID] = cache_get_field_content_int(0"ID");
        
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_PASSWORD"Login""Welcome player!\nYour account has been found in our database. Please fill in your password:""Login""Quit");
    }
    else
    {
        
ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_PASSWORD"Register""Welcome player!\nYour account has not been registered yet. Please fill in your desired password:""Register""Quit");
    }
    return 
true;
}
forward OnPlayerLoad(playerid);
public 
OnPlayerLoad(playerid)
{
    
Player[playerid][Admin] = cache_get_field_content_int(0"Admin");
    
Player[playerid][VIP] = cache_get_field_content_int(0"VIP");
    
Player[playerid][Money] = cache_get_field_content_int(0"Money");
    
Player[playerid][PosX] = cache_get_field_content_float(0"PosX");
    
Player[playerid][PosY] = cache_get_field_content_float(0"PosY");
    
Player[playerid][PosZ] = cache_get_field_content_float(0"PosZ");
    
Player[playerid][PosA] = cache_get_field_content_float(0"PosA");
    
TogglePlayerSpectating(playeridfalse);
    
GivePlayerMoney(playeridPlayer[playerid][Money]);
    
SetSpawnInfo(playerid023Player[playerid][PosX], Player[playerid][PosY], Player[playerid][PosZ], Player[playerid][PosA], 000000);
    
SpawnPlayer(playerid);
    
SendClientMessage(playerid, -1"You have successfully logged in.");
    return 
true;
}
forward OnAccountRegister(playerid);
public 
OnAccountRegister(playerid)
{
    
Account[playerid][ID] = cache_insert_id();
    
printf("[Registration] New account registered. Database ID: [%d]"Account[playerid][ID]);
    
TogglePlayerSpectating(playeridfalse);
    
SetSpawnInfo(playerid023START_XSTART_YSTART_ZSTART_A000000);
    
SpawnPlayer(playerid);
    return 
true;
}
forward OnPlayerRegister(playerid);
public 
OnPlayerRegister(playerid)
{
    
Player[playerid][ID] = cache_insert_id();
    
printf("[Registration] New account registered. Database ID: [%d]"Player[playerid][ID]);
    
TogglePlayerSpectating(playeridfalse);
    
SetSpawnInfo(playerid023START_XSTART_YSTART_ZSTART_A000000);
    
SpawnPlayer(playerid);
    return 
true;

Reply
#6

This is your problem:
PHP код:
            mysql_format(mysqlquerysizeof(query), "INSERT INTO `account_data` (`Name`, `Password`, `IP`) VALUES ('%e', '%e', '%e')"playernameAccount[playerid][Password], playerip); 
            
mysql_format(mysqlquery2sizeof(query), "INSERT INTO `player_data` (`Name`, `Admin`, `VIP`, `Money`, `PosX`, `PosY`, `PosZ`, `PosA`) VALUES ('%e', 0, 0, 0, %f, %f, %f, %f)"playernameSTART_XSTART_YSTART_ZSTART_A); 
            
mysql_tquery(mysqlquery"OnAccountRegister""i"playerid); 
            
mysql_tquery(mysqlquery2"OnPlayerRegister""i"playerid); 
You're adding a row with data to two different tables (tables which have the same purpose?) and you call two different functions (which have the same purpose!) at the same time. Merge the queries and adjust your database accordingly.
Reply
#7

Hi Andy,

I want to separate it, may you can help me improve this? If the account is created it will automatically generate id and name on a no data database.
Reply
#8

Your table:
Код:
CREATE TABLE IF NOT EXISTS `account_data` (

	`ID` INT(24) NOT NULL AUTO_INCREMENT,
	`Password` VARCHAR(129) NOT NULL,
	`IP` VARCHAR(16) NOT NULL,
	`Admin` INT(4) NOT NULL,
	`VIP` INT(2) NOT NULL,
	`Money` INT(24) NOT NULL,
	`PosX` FLOAT(24) NOT NULL,
	`PosY` FLOAT(24) NOT NULL,
	`PosZ` FLOAT(24) NOT NULL,
	`PosA` FLOAT(24) NOT NULL,
 
	PRIMARY KEY(`ID`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;
PHP код:
mysql_format(mysqlquerysizeof(query), "INSERT INTO account_data (`Name`, `Password`, `IP`, `Admin`, `VIP`, `Money`, `PosX`, `PosY`, `PosZ`, `PosA`) VALUES ('%e', '%e', '%e', 0, 0, 0, %f, %f, %f, %f)"playernameAccount[playerid][Password], playeripSTART_XSTART_YSTART_ZSTART_A);
mysql_tquery(mysqlquery"OnAccountRegister""i"playerid); 
Best is to add default values to columns that have a non-constant value upon creating a row. Columns such as: Admin, VIP, Money, PosX to PosZ all have constant values when you create the player's row.

Your table would look like so:
Код:
CREATE TABLE IF NOT EXISTS `account_data` (

	`ID` INT(24) NOT NULL AUTO_INCREMENT,
	`Password` VARCHAR(129) NOT NULL,
	`IP` VARCHAR(16) NOT NULL,
	`Admin` INT(4) NOT NULL DEFAULT '0',
	`VIP` INT(2) NOT NULL DEFAULT '0',
	`Money` INT(24) NOT NULL DEFAULT '0',
	`PosX` FLOAT(24) NOT NULL DEFAULT '2492.0830',
	`PosY` FLOAT(24) NOT NULL DEFAULT '-1665.6895 ',
	`PosZ` FLOAT(24) NOT NULL DEFAULT '13.3438',
	`PosA` FLOAT(24) NOT NULL DEFAULT '93.1757',
 
	PRIMARY KEY(`ID`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;
And your query would then result into:
PHP код:
mysql_format(mysqlquerysizeof(query), "INSERT INTO account_data (`Name`, `Password`, `IP`) VALUES ('%e', '%e', '%e')"playernameAccount[playerid][Password], playerip);
mysql_tquery(mysqlquery"OnAccountRegister""i"playerid); 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)