PDA

View Full Version : Probelm with MySQL register.



nightbot
23rd July 2016, 02:27
Hello, i've been trying to make a register command. But it allows 2 usernames on database without having an error ingame.
I'm tired working on this since 4 days with no success.
These are codes guys
CreateAdmin(login, password)
{
maps\mp\gametypes\_mysql::asyncQuery("INSERT INTO admins (login, password) VALUES ('" + maps\mp\gametypes\_mysql::stripString(login) + "', '" + maps\mp\gametypes\_mysql::stripString(password) + "')");
self thread tellMessage("Succes! Your account has been created.");
}
SearchForLogin(login)
{
maps\mp\gametypes\_mysql::asyncQuery("SELECT login FROM admins WHERE login = '" + maps\mp\gametypes\_mysql::stripString(login) + "' LIMIT 1", ::checkLogin);
}

checkLogin(rows, args)
{
if(isDefined(rows) && isDefined(rows[0]) && isDefined(rows[0]["login"]) && isDefined(self))
{

self tellMessage("I'm sorry, but login " + rows[0]["login"] + "^7 already exist. Choose another one.");
return;
}

}

The register statement :

case "register":
if(args[2].size >= 3 && args[2].size <= 20)
{
if(args[3].size >= 5 && args[3].size <= 25)
{
SearchForLogin(args[2]);
wait 3;
if(isDefined(args[2]) && isDefined(args[3]))
checklogin(args[2]);
createadmin(args[2], args[3]);
return;
}
else
{
self thread tellMessage("Password is too short or too long. Min 5 characters, max 25.");
return;
}
}
self thread tellMessage("Login is too short or too long. Min 3 characters, max 20.");
return;

Please guys help !

nightbot
23rd July 2016, 07:29
bump
10char

maxdamage99
23rd July 2016, 08:52
Omg, where query's? :DD

nightbot
23rd July 2016, 09:01
could you fix it for me?

maxdamage99
23rd July 2016, 09:23
i no use async, i can fix by simple
mysql_query

nightbot
23rd July 2016, 09:33
Ok give it

Mitch
23rd July 2016, 09:36
could you fix it for me?

You can restrict double values with UNIQUE (http://www.w3schools.com/sql/sql_unique.asp).
Your problem isn't the database, but how you process the results.
E.g.


case "register":
hasLogin = searchForLogin(username);
if (!hasLogin) {
createAdmin(username, password);
}

Note: this is not working code that you can just copy and paste and use. Just an example.

nightbot
23rd July 2016, 09:38
Ahaa ! But can you give me atleast a good start for it ? I'm not that good at mysql things tbh mitch

IzNoGoD
23rd July 2016, 10:20
Create a UNIQUE key constraint in your mysql db. Then use a stored procedure (function) to create new accounts, which use a INSERT IGNORE. Then double-check on last_insert_id inside that function. Return something if successful (possibly the user's ID), and an error code if unsuccessful (-1 should do a decent job at being an error code)

nightbot
23rd July 2016, 16:33
I'm not that good at mysql things tbh

Every single thing u said didn't understand anything from. Maybe a small tutorial or some links.
If u wants to help me i will just have 3 columns login password rank in admins table.

Paho
23rd July 2016, 19:45
Every single
Every single day - single hour
I can see your face - single day

nightbot
23rd July 2016, 20:00
Ur comment ain't helping here ..

YuriJurek
23rd July 2016, 20:42
Killtube's being taken over by nuubz...

I had to say it xD

nightbot
23rd July 2016, 22:35
none will help then :/

nightbot
24th July 2016, 08:15
@IzNoGod i managed to make the login unique and primary. But i got some problems showing errors.
If there is an account, it returns a self thread tellmessage("account exist");
but if there is no account, it doesn't process the query.
I think it's from my register case.

case "register":
if(args[2].size >= 3 && args[2].size <= 20)
{
haslogin = searchforlogin(args[2]);
if(haslogin == true)
{
if(args[3].size >= 5 && args[3].size <= 25)
{
return;
}
if(haslogin == false)
{
iprintlnbold("ye");
createadmin(args[2], args[3]);
return;
}
}
self thread tellmessage("Password is too short or too long. Min 5 characters, max 25.");
return;
}
self thread tellMessage("Login is too short or too long. Min 3 characters, max 20.");
return;

IzNoGoD
24th July 2016, 08:25
Fix your indentation.

You'll see this:

case "register":
if(args[2].size >= 3 && args[2].size <= 20)
{
haslogin = searchforlogin(args[2]);
if(haslogin == true)
{
if(args[3].size >= 5 && args[3].size <= 25)
{
return;
}
if(haslogin == false)
{
iprintlnbold("ye");
createadmin(args[2], args[3]);
return;
}
}
self thread tellmessage("Password is too short or too long. Min 5 characters, max 25.");
return;
}
self thread tellMessage("Login is too short or too long. Min 3 characters, max 20.");
return;

the if(haslogin == false) is inside the if(haslogin == true)

maxdamage99
24th July 2016, 09:11
WTF? Izno..


if(param==true)
{
/*...*/
if(param==false)
{
/*...*/
}
/*...*/
}


Try:


case "register":
{
if(args[2].size >= 3 && args[2].size <= 20)
{
if(args[3].size >= 5 && args[3].size <= 25)
{
haslogin = searchforlogin(args[2]);
if(haslogin)
self iprintlnbold("Login taked!");
else
if(!haslogin)
{
self iprintlnbold("Login no register!");
createadmin(args[2], args[3]);
}
}
else
self thread tellmessage("Password is too short or too long. Min 5 characters, max 25.");
}
else
self thread tellMessage("Login is too short or too long. Min 3 characters, max 20.");
return;
}

nightbot
24th July 2016, 09:31
Thx for ur help.
Even when the user is available i get that Login taked !

This is my searchforlogin thread

SearchForLogin(login)
{
maps\mp\gametypes\_mysql::asyncQuery("SELECT login FROM admins WHERE login = '" + maps\mp\gametypes\_mysql::stripString(login) + "' LIMIT 1", ::checkLogin);
}

checkLogin(rows, args)
{
if(isDefined(rows) && isDefined(rows[0]) && isDefined(rows[0]["login"]) && isDefined(self))
{

self tellMessage("I'm sorry, but login " + rows[0]["login"] + "^7 already exist. Choose another one.");
return;
}

}

This is my query for creating the account:


CreateAdmin(login, pass)
{
maps\mp\gametypes\_mysql::asyncQuery("INSERT INTO admins (login, pass) VALUES ('" + maps\mp\gametypes\_mysql::stripString(login) + "', '" + maps\mp\gametypes\_mysql::stripString(pass) + "')");
self thread tellmessage("success");
}

Maybe a problem in searchforlogin ?

IzNoGoD
24th July 2016, 09:57
WTF? Izno..



I replied with HIS code properly indented to show that it is wrong.

nightbot
24th July 2016, 09:58
IzNoGod it would be cool if you fix it :s

serthy
24th July 2016, 10:54
SearchForLogin() doesnt return a value.

As I wrote you on blizo.de, you should definately watch/read some basic scripting tutorials first like https://killtube.org/showthread.php?359-VIDEO-rotating-brushmodels

nightbot
25th July 2016, 00:42
case "register":
login = args[2];
pass = args[3];
if(args[2].size >= 3 && args[2].size <= 20)
{
if(args[3].size >= 5 && args[3].size <= 25)
{
iprintlnbold("login check");
if(searchforlogin(args[2]) == true)
{
iprintlnbold("Name exist !");
return;
}
if(searchforlogin(args[2]) == false)
{
iprintlnbold("TEST");
wait 5;
self thread createadmin(args[2], args[3]);
self iprintlnbold("OK");
return;
}
return;
}
self thread tellmessage("Password is too short or too long. Min 5 characters, max 25.");
return;
}
self thread tellMessage("Login is too short or too long. Min 3 characters, max 20.");
return;


CreateAdmin(login, pass)
{
self thread tellmessage("success");
return maps\mp\gametypes\_mysql::asyncQuery("INSERT INTO admins (login, pass) VALUES ('" + maps\mp\gametypes\_mysql::stripString(login) + "', '" + maps\mp\gametypes\_mysql::stripString(pass) + "')");
}

SearchForLogin(login)
{
maps\mp\gametypes\_mysql::asyncQuery("SELECT login FROM admins WHERE login = '" + maps\mp\gametypes\_mysql::stripString(login) + "' LIMIT 1", ::checkLogin);
}

checkLogin(rows, args)
{
if(isDefined(rows) && isDefined(rows[0]) && isDefined(rows[0]["login"]) && isDefined(self))
{
self thread tellmessage("already exist");
return true;
}
return false;

}

Guys please correct it for me. i'm just bored of doing all solutions in my mind without success.
It just doesn't create account when the login is available.
Please izno,voron, anyone who can help .. :s

IzNoGoD
25th July 2016, 08:09
Something like this might work:



checklogin(username, password)
{
args = [];
args[0] = "login";
self asyncQuery("SELECT userid FROM users WHERE username = '" + stripstring(username) + "' AND password = '" + stripstring(password) + "'", ::receivelogin);
}

recievelogin(rows, args)
{
if(rows.size && isdefined(rows[0]) && isdefined(rows[0]["userid"]))
{
self.userid = int(rows[0]["userid"]);
if(args[0] == "login")
self iprintlnbold("Login completed");
else
self iprintlnbold("Register succesful");
}
else
{
if(args[0] == "login")
self iprintlnbold("Login failed. Wrong password?");
else
self iprintlnbold("Register failed. User already exists");
}
}

registeraccount(username, password)
{
args = [];
args[0] = "register";
self asyncQuery("SELECT create_new_account() AS `userid`", ::receivelogin, args);
}


mysql function:


CREATE DEFINER=`root`@`127.0.0.1` FUNCTION `createNewAccount`(`_username` CHAR(50), `_password` CHAR(50))
RETURNS int(11)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE _userid INT DEFAULT NULL;
SELECT LAST_INSERT_ID(NULL) INTO _userid;
INSERT IGNORE INTO users (userid, login, password) VALUES (NULL, _login, _password);
SELECT LAST_INSERT_ID() INTO _userid;
RETURN _userid;
END

Untested.

Needs a table "users" with primary key "userid" with auto-increment, a unique login (char(50)) and non-unique password (char(50))

nightbot
25th July 2016, 08:34
How to execute that mysql function ? i tried with heidisql/phpmyadmin sql executer but it gives me error

SQL query:

CREATE DEFINER = `root`@`127.0.0.1` FUNCTION `createNewAccount` (

`_username` CHAR( 50 ) ,
`_password` CHAR( 50 )
) RETURNS INT( 11 ) LANGUAGE SQL NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' BEGIN DECLARE _userid INT DEFAULT NULL ;

MySQL said: Documentation

#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 '' at line 9

nightbot
25th July 2016, 08:41
Sorry for double post. i managed to execute these functions by mysql -u root -p , then delimiter // , i wrote the fuctions then i didi end //

nightbot
25th July 2016, 09:28
I did everything. Created the database,table,columns, but the problem now is the case "register", i don't know how to order the queries.
i tried this but it gives me user already exists everytime

case "register":
if(args[2].size >= 3 && args[2].size <= 20)
{
if(args[3].size >= 5 && args[3].size <= 25)
{
registeraccount(args[2], args[3]);
return;
}
self thread tellmessage("pw too short/long");
return;
}
self thread tellmessage("user too short/long");
thx izno and sorry for annoying u ik u are bored. but can u help me pls ?

IzNoGoD
25th July 2016, 09:37
registeraccount(username, password)
{
args = [];
args[0] = "register";
self asyncQuery("SELECT create_new_account() AS `userid`", ::receivelogin, args);
}
->



registeraccount(username, password)
{
args = [];
args[0] = "register";
self asyncQuery("SELECT create_new_account('" + stripstring(username) + "', '" + stripstring(password) + "') AS `userid`", ::receivelogin, args);
}

nightbot
25th July 2016, 09:47
Still the same error.
registration failed, already exist

IzNoGoD
25th July 2016, 09:58
are you sure your table has a primarykey with autoincrement called userid?

nightbot
25th July 2016, 10:00
1195

check is izno.

IzNoGoD
25th July 2016, 11:23
As what user are you logging in from cod?

nightbot
25th July 2016, 11:57
U mean when i do !register username pw ? i use !register nightbot andpwhere

also, im just trying to make ONLY a register system, i'll do login soon, but not now;

I think it's from the case "register" :s

IzNoGoD
25th July 2016, 12:00
no, i mean mysql user

nightbot
25th July 2016, 12:03
root !
10chars

IzNoGoD
25th July 2016, 12:04
Interesting, addme on steam: http://steamcommunity.com/id/IzNoGoD/

nightbot
25th July 2016, 12:13
I can't add u , it requires 5$ lol. If you got any other way to contact u. Pm with it

Ni3ls
25th July 2016, 13:36
Start making your own scripts

IzNoGoD
25th July 2016, 17:43
Helped him through teamviewer. He didnt do any debugging, just copy pasted my code. Problem was in some typos i made in my quick n dirty code.

Learn to debug.

nightbot
25th July 2016, 18:46
u are a legend :')

YuriJurek
25th July 2016, 20:09
u are a legend :')

Thats a great underestimate...