MySQL Register/Login system problems
#1

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(playeridCOLOR_WHITE);
    new 
name[MAX_PLAYER_NAME+1],
        
string[128];
    
GetPlayerName(playeridnamesizeof(name));
    
format(stringsizeof(string), "SELECT * FROM 'users' WHERE 'username' = '%s'"name);
    
mysql_query(string);
    
mysql_store_result();
    if(
mysql_num_rows() <= 0RegisterPlayer(playerid);
    else if(
mysql_num_rows() >= 1LoginPlayer(playerid);
    
mysql_free_result();
    return 
1;

RegisterPlayer:
PHP код:
RegisterPlayer(playerid)
{
    
ShowPlayerDialog(playerid1DIALOG_STYLE_INPUT"Register""Please enter the password for your new account.""Submit""Cancel");
    return 
1;

LoginPlayer:
PHP код:
LoginPlayer(playerid)
{
    
ShowPlayerDialog(playerid2DIALOG_STYLE_INPUT"Login""This account is registered, please enter the password.""Submit""Cancel");
    return 
1;

OnDialogResponse:
PHP код:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
if(
response)
{
    switch(
dialogid)
    {
        case 
1:
        {
            if(
strlen(inputtext) < || strlen(inputtext) > 30)
            {
                
SendClientMessage(playeridCOLOR_RED"[Error] The password's length must be between 3~30");
                
ShowPlayerDialog(playerid1DIALOG_STYLE_INPUT"Register""Password too short/long, enter a new one!""Submit""Cancel");
                return 
1;
            }
            new 
string[128],
                
name[MAX_PLAYER_NAME+1];
            
GetPlayerName(playeridnamesizeof(name));
            
format(stringsizeof(string), "INSERT INTO 'users' ('username', 'password') VALUES ('%s', '%s')"nameinputtext);
            
mysql_query(string);
            
SendClientMessage(playeridCOLOR_LIGHTBLUE"[SUCCESS] Your account has been created, please log in now.");
            
LoginPlayer(playerid);
        }
        case 
2:
        {
            new 
string[128],
                
name[MAX_PLAYER_NAME+1];
            
GetPlayerName(playeridnamesizeof(name));
            
format(stringsizeof(string), "SELECT * FROM 'users' WHERE 'username' = '%s' AND 'password' = '%s'"nameinputtext);
               
mysql_query(string);
               
mysql_store_result();
               if(
mysql_num_rows() <= 0)
            {
                
SendClientMessage(playeridCOLOR_RED"[Error] Wrong password!");
                
ShowPlayerDialog(playerid2DIALOG_STYLE_INPUT"Login""Wrong password, enter the correct one!""Submit""Cancel");
                return 
1;
            }
            else 
SendClientMessage(playeridCOLOR_WHITE"[SUCCESS] You have successfully logged in!");
        }
    }
}
return 
1;

Reply
#2

Well, please post part of register query and login query in your debug.txt , if you dont have enabled log, do it with
pawn Код:
mysql_debug(1);
in OnGameModeInit
Reply
#3

PHP код:
[13:36:56MySQL Debugging activated (09/04/13)

[
13:36:56] ---------------------------

[
13:36:56]  

[
13:37:27] >> mysql_queryConnection handle)

[
13:37:27CMySQLHandler::Query(SELECT FROM 'users' WHERE 'username' 'mayne') - An error has occured. (Error ID1064You have an error in your SQL syntaxcheck 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_resultConnection handle)

[
13:37:27CMySQLHandler::StoreResult() - No data to store.

[
13:37:27] >> mysql_num_rowsConnection handle)

[
13:37:27CMySQLHandler::NumRows() - You cannot call this function now. (ReasonDead Connection)

[
13:37:27] >> mysql_free_resultConnection handle)

[
13:37:27CMySQLHandler::FreeResult() - The result is already empty.

[
13:37:29] >> mysql_queryConnection handle)

[
13:37:29CMySQLHandler::Query(SELECT FROM 'users' WHERE 'username' 'mayne' AND 'password' '456') - An error has occured. (Error ID1064You have an error in your SQL syntaxcheck 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_resultConnection handle)

[
13:37:29CMySQLHandler::StoreResult() - No data to store.

[
13:37:29] >> mysql_num_rowsConnection handle)

[
13:37:29CMySQLHandler::NumRows() - You cannot call this function now. (ReasonDead Connection)

[
13:37:30] >> mysql_queryConnection handle)

[
13:37:30CMySQLHandler::Query(SELECT FROM 'users' WHERE 'username' 'mayne' AND 'password' '456') - An error has occured. (Error ID1064You have an error in your SQL syntaxcheck 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_resultConnection handle)

[
13:37:30CMySQLHandler::StoreResult() - No data to store.

[
13:37:30] >> mysql_num_rowsConnection handle)

[
13:37:30CMySQLHandler::NumRows() - You cannot call this function now. (ReasonDead Connection
Reply
#4

Код:
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.
Reply
#5

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);
Reply
#6

Thanks it now works
Reply
#7

Edit: I am too late...
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)