MySQL Register/Login system problems -
Tomer!.$ - 04.09.2013
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:
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;
}
RegisterPlayer:
PHP код:
RegisterPlayer(playerid)
{
ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Register", "Please enter the password for your new account.", "Submit", "Cancel");
return 1;
}
LoginPlayer:
PHP код:
LoginPlayer(playerid)
{
ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Login", "This account is registered, please enter the password.", "Submit", "Cancel");
return 1;
}
OnDialogResponse:
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;
}
Re: MySQL Register/Login system problems -
Army - 04.09.2013
Well, please post part of register query and login query in your debug.txt , if you dont have enabled log, do it with
in OnGameModeInit
Re: MySQL Register/Login system problems -
Tomer!.$ - 04.09.2013
PHP код:
[13:36:56] MySQL Debugging activated (09/04/13)
[13:36:56] ---------------------------
[13:36:56]
[13:37:27] >> mysql_query( Connection handle: 1 )
[13:37:27] CMySQLHandler::Query(SELECT * FROM 'users' WHERE 'username' = 'mayne') - An error has occured. (Error ID: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'username' = 'mayne'' at line 1)
[13:37:27] >> mysql_store_result( Connection handle: 1 )
[13:37:27] CMySQLHandler::StoreResult() - No data to store.
[13:37:27] >> mysql_num_rows( Connection handle: 1 )
[13:37:27] CMySQLHandler::NumRows() - You cannot call this function now. (Reason: Dead Connection)
[13:37:27] >> mysql_free_result( Connection handle: 1 )
[13:37:27] CMySQLHandler::FreeResult() - The result is already empty.
[13:37:29] >> mysql_query( Connection handle: 1 )
[13:37:29] CMySQLHandler::Query(SELECT * FROM 'users' WHERE 'username' = 'mayne' AND 'password' = '456') - An error has occured. (Error ID: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'username' = 'mayne' AND 'password' = '456'' at line 1)
[13:37:29] >> mysql_store_result( Connection handle: 1 )
[13:37:29] CMySQLHandler::StoreResult() - No data to store.
[13:37:29] >> mysql_num_rows( Connection handle: 1 )
[13:37:29] CMySQLHandler::NumRows() - You cannot call this function now. (Reason: Dead Connection)
[13:37:30] >> mysql_query( Connection handle: 1 )
[13:37:30] CMySQLHandler::Query(SELECT * FROM 'users' WHERE 'username' = 'mayne' AND 'password' = '456') - An error has occured. (Error ID: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'username' = 'mayne' AND 'password' = '456'' at line 1)
[13:37:30] >> mysql_store_result( Connection handle: 1 )
[13:37:30] CMySQLHandler::StoreResult() - No data to store.
[13:37:30] >> mysql_num_rows( Connection handle: 1 )
[13:37:30] CMySQLHandler::NumRows() - You cannot call this function now. (Reason: Dead Connection)
Re: MySQL Register/Login system problems -
Vince - 04.09.2013
Код:
An error has occured. (Error ID: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'username' = 'mayne'' at line 1)
Reading is a profession in itself. You ONLY use the quotes (') for strings. Table and field names need only be wrapped in
backticks (`) if they're SQL keywords.
Re: MySQL Register/Login system problems -
Army - 04.09.2013
try edit:
pawn Код:
format(string, sizeof(string), "SELECT * FROM 'users' WHERE 'username' = '%s'", name);
to:
format(string, sizeof(string), "SELECT * FROM `users` WHERE `username` = '%s'", name);
format(string, sizeof(string), "INSERT INTO 'users' ('username', 'password') VALUES ('%s', '%s')", name, inputtext);
to:
format(string, sizeof(string), "INSERT INTO `users` (username, `password`) VALUES ('%s', '%s')", name, inputtext);
format(string, sizeof(string), "SELECT * FROM 'users' WHERE 'username' = '%s' AND 'password' = '%s'", name, inputtext);
to:
format(string, sizeof(string), "SELECT * FROM `users` WHERE `username` = '%s' AND `password` = '%s'", name, inputtext);
edit: btw better to use MD5 coding for passwords, for example:
pawn Код:
format(string, sizeof(string), "INSERT INTO `users` (username, `password`) VALUES ('%s', MD5('%s'))", name, inputtext);
format(string, sizeof(string), "SELECT * FROM `users` WHERE `username` = '%s' AND `password` = MD5('%s')", name, inputtext);
Re: MySQL Register/Login system problems -
Tomer!.$ - 04.09.2013
Thanks it now works
Re: MySQL Register/Login system problems -
Dragonsaurus - 04.09.2013
Edit: I am too late...