Raised This Month: $51 Target: $400
 12% 

insert into (or update) mysql fails because of special characters


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
TheBladerX
Member
Join Date: Dec 2012
Location: Slovakia
Old 02-07-2019 , 10:38   insert into (or update) mysql fails because of special characters
Reply With Quote #1

Hey, how do I fix that problem? For example player's name is -*KURCZACY*- tesak
and console pops
Code:
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 ''5' at line 1
. (All players with normal names are uploaded correctly to database, so it is because of these special chars)

I have following codes to insert, update and select:

PHP Code:
new table1[] = "CREATE TABLE IF NOT EXISTS `codexp` ( `user` VARCHAR(33) NOT NULL , `trieda` INT NOT NULL , `level` INT NOT NULL , PRIMARY KEY (`user`)) ENGINE = InnoDB;" 

formatex(Querycharsmax(Query), "INSERT INTO `codexp` (`user`, `trieda`, `level`) VALUES ('%s-%i', '%i', '%i') ON DUPLICATE KEY UPDATE `level` = '%i'"nazwa_gracza[id], klasa_gracza[id], klasa_gracza[id], poziom_gracza[id], poziom_gracza[id]) 

formatex(Querycharsmax(Query), "SELECT * FROM codexp WHERE user='%s-%i'"nazwa_gracza[id], klasa_gracza[id])

formatex(Querycharsmax(Query), "UPDATE `codexp` SET `level` = '%i', `trieda` = '%i' WHERE `user` = '%s-%i';"poziom_gracza[id], klasa_gracza[id], nazwa_gracza[id], klasa_gracza[id]) 
I am also aware of that the problem can be caused by %s-%i dash, but how can I fix it, except by adding other character than dash?

Last edited by TheBladerX; 02-07-2019 at 10:53.
TheBladerX is offline
Airkish
AlliedModders Donor
Join Date: Apr 2016
Location: Lithuania
Old 02-07-2019 , 11:51   Re: insert into (or update) mysql fails because of special characters
Reply With Quote #2

PHP Code:
new szText[256];

formatex(szTextsizeof(szText), "%s"nazwa_gracza[id]);
replace_all(szTextsizeof(szText), "'""\'");
formatex(Querycharsmax(Query), "SELECT * FROM codexp WHERE user='%s-%i'"szTextklasa_gracza[id]);
formatex(Querycharsmax(Query), "UPDATE `codexp` SET `level` = '%i', `trieda` = '%i' WHERE `user` = '%s-%i';"poziom_gracza[id], klasa_gracza[id], szTextklasa_gracza[id]) 
__________________

Last edited by Airkish; 02-07-2019 at 12:03.
Airkish is offline
TheBladerX
Member
Join Date: Dec 2012
Location: Slovakia
Old 02-07-2019 , 12:11   Re: insert into (or update) mysql fails because of special characters
Reply With Quote #3

Quote:
Originally Posted by Airkish View Post
PHP Code:
new szText[256];

formatex(szTextsizeof(szText), "%s"nazwa_gracza[id]);
replace_all(szTextsizeof(szText), "'""\'");
formatex(Querycharsmax(Query), "SELECT * FROM codexp WHERE user='%s-%i'"szTextklasa_gracza[id]);
formatex(Querycharsmax(Query), "UPDATE `codexp` SET `level` = '%i', `trieda` = '%i' WHERE `user` = '%s-%i';"poziom_gracza[id], klasa_gracza[id], szTextklasa_gracza[id]) 
Okay, seems like everything works perfectly. Thank you for now.

Last edited by TheBladerX; 02-07-2019 at 12:36.
TheBladerX is offline
eat1k
Senior Member
Join Date: Apr 2018
Old 02-07-2019 , 12:46   Re: insert into (or update) mysql fails because of special characters
Reply With Quote #4

PHP Code:
mysql_escape_string(dest[],len)
{
    
//copy(dest, len, source);
    
replace_all(dest,len,"\\","\\\\");
    
replace_all(dest,len,"\0","\\0");
    
replace_all(dest,len,"\n","\\n");
    
replace_all(dest,len,"\r","\\r");
    
replace_all(dest,len,"\x1a","\Z");
    
replace_all(dest,len,"'","''");
    
replace_all(dest,len,"^"","^"^"");

__________________

Last edited by eat1k; 02-07-2019 at 12:47.
eat1k is offline
TheBladerX
Member
Join Date: Dec 2012
Location: Slovakia
Old 02-07-2019 , 12:50   Re: insert into (or update) mysql fails because of special characters
Reply With Quote #5

Quote:
Originally Posted by eat1k View Post
PHP Code:
mysql_escape_string(dest[],len)
{
    
//copy(dest, len, source);
    
replace_all(dest,len,"\\","\\\\");
    
replace_all(dest,len,"\0","\\0");
    
replace_all(dest,len,"\n","\\n");
    
replace_all(dest,len,"\r","\\r");
    
replace_all(dest,len,"\x1a","\Z");
    
replace_all(dest,len,"'","''");
    
replace_all(dest,len,"^"","^"^"");

Should I put it that way as it's shown before?
Under this?
PHP Code:
replace_all(szTextsizeof(szText), "'""\'"); 
And one more question, what is the difference between these two?
"'", "\'" and "'","''"

Last edited by TheBladerX; 02-07-2019 at 12:54.
TheBladerX is offline
eat1k
Senior Member
Join Date: Apr 2018
Old 02-07-2019 , 12:55   Re: insert into (or update) mysql fails because of special characters
Reply With Quote #6

Add this function to your code and use it instead of "replace_all(...".
PHP Code:
mysql_escape_string(szTextcharsmax(szText); 
__________________
eat1k is offline
TheBladerX
Member
Join Date: Dec 2012
Location: Slovakia
Old 02-07-2019 , 14:09   Re: insert into (or update) mysql fails because of special characters
Reply With Quote #7

Quote:
Originally Posted by TheBladerX View Post
And one more question, what is the difference between these two?
"'", "\'" and "'","''"
TheBladerX is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode