Raised This Month: $2 Target: $400
 0% 

SQL Error when name contains apostrophe


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mlk27
Veteran Member
Join Date: May 2008
Old 06-19-2009 , 19:29   SQL Error when name contains apostrophe
Reply With Quote #1

Code:
SQL_PrepareQuery(SqlConnection, "INSERT INTO rm_rates VALUES('%s', '%s')", steamid, name)
This will fail when player name contains apostrophe symbol, eg Rzzy 'o' B

Any idea how to fix this?
Mlk27 is offline
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 06-19-2009 , 19:53   Re: SQL Error when name contains apostrophe
Reply With Quote #2

search for every ' in the name and put \ before
__________________
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
Mlk27
Veteran Member
Join Date: May 2008
Old 06-19-2009 , 20:27   Re: SQL Error when name contains apostrophe
Reply With Quote #3

Oh..Is this corrent?

Code:
	get_user_name(id, name, 31)

	if(containi(name, "'"))
		replace_all(name, 31, "'", "\'")
Mlk27 is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 06-21-2009 , 02:23   Re: SQL Error when name contains apostrophe
Reply With Quote #4

Quote:
Originally Posted by Mlk27 View Post
Oh..Is this corrent?

Code:
    get_user_name(id, name, 31)

    if(containi(name, "'"))
        replace_all(name, 31, "'", "\'")
You don't need that.

Just do:
Code:
SQL_PrepareQuery(SqlConnection, "INSERT INTO rm_rates VALUES(^"%s^",^"%s^")", steamid, name)
(Names can't have quotes so you would be fine and will not waste resources)
__________________

Last edited by joaquimandrade; 06-21-2009 at 20:46.
joaquimandrade is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 06-21-2009 , 20:39   Re: SQL Error when name contains apostrophe
Reply With Quote #5

Single quotes will still throw a syntax error. He has it right.
Spunky is offline
Send a message via AIM to Spunky
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 06-21-2009 , 20:45   Re: SQL Error when name contains apostrophe
Reply With Quote #6

Quote:
Originally Posted by Spunky View Post
Single quotes will still throw a syntax error. He has it right.
Prove it
__________________
joaquimandrade is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 06-22-2009 , 20:27   Re: SQL Error when name contains apostrophe
Reply With Quote #7

Code:
#include <amxmodx> #include <sqlx> new Handle:g_hSQLTuple public plugin_init() {     register_plugin("SQL Syntax Error", "1.0", "Spunky")     g_hSQLTuple = SQL_MakeDbTuple("localhost", "root", "test", "arp")     new szQuery[256]     formatex(szQuery, 255, "CREATE TABLE IF NOT EXISTS synerrorlolar (Proved VARCHAR(20), IT VARCHAR(20), UNIQUE KEY (Proved))")     SQL_ThreadQuery(g_hSQLTuple, "fnQueryHandle", szQuery)     new szName[32]     szName = "Spunky 'lolar' Test"     formatex(szQuery, 255, "INSERT INTO synerrorlolar VALUES ('%s', 'You fail.')", szName)     SQL_ThreadQuery(g_hSQLTuple, "fnQueryHandle", szQuery) } public fnQueryHandle(FailState, Handle:hQuery, szError[], iErrorCode, iData[], iDataSize) {     if (FailState == TQUERY_CONNECT_FAILED)         set_fail_state("Could not connect to SQL database!")     else if (FailState == TQUERY_QUERY_FAILED)         set_fail_state("Query failed to execute!")     return PLUGIN_CONTINUE }

Quote:
L 06/22/2009 - 20:26:12: [AMXX] Plugin ("sqltest.amxx") is setting itself as failed.
L 06/22/2009 - 20:26:12: [AMXX] Plugin says: Query failed to execute!
L 06/22/2009 - 20:26:12: [AMXX] Displaying debug trace (plugin "sqltest.amxx")
L 06/22/2009 - 20:26:12: [AMXX] Run time error 1: forced exit
L 06/22/2009 - 20:26:12: [AMXX] [0] sqltest.sma::fnQueryHandle (line 31)
Table exists:
[IMG]http://img31.**************/img31/7528/pwntz.jpg[/IMG]

Last edited by Spunky; 06-22-2009 at 20:38.
Spunky is offline
Send a message via AIM to Spunky
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 06-22-2009 , 20:30   Re: SQL Error when name contains apostrophe
Reply With Quote #8

Quote:
Originally Posted by Spunky View Post
Code:
#include <amxmodx> #include <sqlx> new Handle:g_hSQLTuple

public plugin_init() { &nbsp;&nbsp;&nbsp;&nbsp;register_plugin("SQL Syntax Error", "1.0", "Spunky") &nbsp;&nbsp;&nbsp;&nbsp;g_hSQLTuple = SQL_MakeDbTuple("localhost", "root", "test", "arp") &nbsp;&nbsp;&nbsp;&nbsp;new szQuery[256] &nbsp;&nbsp;&nbsp;&nbsp;formatex(szQuery, 255, "CREATE TABLE IF NOT EXISTS synerrorlolar (Proved VARCHAR(20), IT VARCHAR(20), UNIQUE KEY (Proved))")
&nbsp;&nbsp;&nbsp;&nbsp;SQL_ThreadQuery(g_hSQLTuple, "fnQueryHandle", szQuery) &nbsp;&nbsp;&nbsp;&nbsp;new szName[32] &nbsp;&nbsp;&nbsp;&nbsp;szName = "Spunky 'lolar' Test" &nbsp;&nbsp;&nbsp;&nbsp;formatex(szQuery, 255, "INSERT INTO synerrorlolar VALUES ('%s', 'You fail.')", szName) &nbsp;&nbsp;&nbsp;&nbsp;SQL_ThreadQuery(g_hSQLTuple, "fnQueryHandle", szQuery) } public fnQueryHandle(FailState, Handle:hQuery, szError[], iErrorCode, iData[], iDataSize) { &nbsp;&nbsp;&nbsp;&nbsp;if (FailState == TQUERY_CONNECT_FAILED) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;set_fail_state("Could not connect to SQL database!") &nbsp;&nbsp;&nbsp;&nbsp;else if (FailState == TQUERY_QUERY_FAILED) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;set_fail_state("Query failed to execute!") &nbsp;&nbsp;&nbsp;&nbsp;return PLUGIN_CONTINUE }

Make this:

PHP Code:
     formatex(szQuery255"INSERT INTO synerrorlolar VALUES ('%s', 'You fail.')"szName
PHP Code:
     formatex(szQuery255"INSERT INTO synerrorlolar VALUES (^"%s^", 'You fail.')"szName
__________________
joaquimandrade is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 06-22-2009 , 20:36   Re: SQL Error when name contains apostrophe
Reply With Quote #9

Hmm, that actually worked, both ways. How about that.
Spunky is offline
Send a message via AIM to Spunky
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 06-22-2009 , 20:42   Re: SQL Error when name contains apostrophe
Reply With Quote #10

Quote:
Originally Posted by Spunky View Post
Hmm, that actually worked, both ways. How about that.
Take your time to swallow it
__________________
joaquimandrade is offline
Reply


Thread Tools
Display Modes

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 09:42.


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