04.09.2013, 10:07
Well, I just learned how to use MySQL for the first time today and this is the first system I'm trying to do with it.
So while trying to make a register/login system I encountered a few issues.
1. Account doesn't really get registered, every time I log into the server with the same name, it always asks me to register, even though I have already registered several times before using this name.
2. When I'm trying to log in, it /ALWAYS/ says the password is wrong, even though the password is actually the same one I used to register with.
OnPlayerConnect:
RegisterPlayer:
LoginPlayer:
OnDialogResponse:
So while trying to make a register/login system I encountered a few issues.
1. Account doesn't really get registered, every time I log into the server with the same name, it always asks me to register, even though I have already registered several times before using this name.
2. When I'm trying to log in, it /ALWAYS/ says the password is wrong, even though the password is actually the same one I used to register with.
OnPlayerConnect:
PHP код:
public OnPlayerConnect(playerid)
{
SetPlayerColor(playerid, COLOR_WHITE);
new name[MAX_PLAYER_NAME+1],
string[128];
GetPlayerName(playerid, name, sizeof(name));
format(string, sizeof(string), "SELECT * FROM 'users' WHERE 'username' = '%s'", name);
mysql_query(string);
mysql_store_result();
if(mysql_num_rows() <= 0) RegisterPlayer(playerid);
else if(mysql_num_rows() >= 1) LoginPlayer(playerid);
mysql_free_result();
return 1;
}
PHP код:
RegisterPlayer(playerid)
{
ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Register", "Please enter the password for your new account.", "Submit", "Cancel");
return 1;
}
PHP код:
LoginPlayer(playerid)
{
ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Login", "This account is registered, please enter the password.", "Submit", "Cancel");
return 1;
}
PHP код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
if(response)
{
switch(dialogid)
{
case 1:
{
if(strlen(inputtext) < 3 || strlen(inputtext) > 30)
{
SendClientMessage(playerid, COLOR_RED, "[Error] The password's length must be between 3~30");
ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Register", "Password too short/long, enter a new one!", "Submit", "Cancel");
return 1;
}
new string[128],
name[MAX_PLAYER_NAME+1];
GetPlayerName(playerid, name, sizeof(name));
format(string, sizeof(string), "INSERT INTO 'users' ('username', 'password') VALUES ('%s', '%s')", name, inputtext);
mysql_query(string);
SendClientMessage(playerid, COLOR_LIGHTBLUE, "[SUCCESS] Your account has been created, please log in now.");
LoginPlayer(playerid);
}
case 2:
{
new string[128],
name[MAX_PLAYER_NAME+1];
GetPlayerName(playerid, name, sizeof(name));
format(string, sizeof(string), "SELECT * FROM 'users' WHERE 'username' = '%s' AND 'password' = '%s'", name, inputtext);
mysql_query(string);
mysql_store_result();
if(mysql_num_rows() <= 0)
{
SendClientMessage(playerid, COLOR_RED, "[Error] Wrong password!");
ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Login", "Wrong password, enter the correct one!", "Submit", "Cancel");
return 1;
}
else SendClientMessage(playerid, COLOR_WHITE, "[SUCCESS] You have successfully logged in!");
}
}
}
return 1;
}