AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   SQL Error when name contains apostrophe (https://forums.alliedmods.net/showthread.php?t=95128)

Mlk27 06-19-2009 19:29

SQL Error when name contains apostrophe
 
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?

Greenberet 06-19-2009 19:53

Re: SQL Error when name contains apostrophe
 
search for every ' in the name and put \ before

Mlk27 06-19-2009 20:27

Re: SQL Error when name contains apostrophe
 
Oh..Is this corrent?

Code:

        get_user_name(id, name, 31)

        if(containi(name, "'"))
                replace_all(name, 31, "'", "\'")


joaquimandrade 06-21-2009 02:23

Re: SQL Error when name contains apostrophe
 
Quote:

Originally Posted by Mlk27 (Post 852652)
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)

Spunky 06-21-2009 20:39

Re: SQL Error when name contains apostrophe
 
Single quotes will still throw a syntax error. He has it right.

joaquimandrade 06-21-2009 20:45

Re: SQL Error when name contains apostrophe
 
Quote:

Originally Posted by Spunky (Post 853847)
Single quotes will still throw a syntax error. He has it right.

Prove it

Spunky 06-22-2009 20:27

Re: SQL Error when name contains apostrophe
 
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]

joaquimandrade 06-22-2009 20:30

Re: SQL Error when name contains apostrophe
 
Quote:

Originally Posted by Spunky (Post 854604)
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


Spunky 06-22-2009 20:36

Re: SQL Error when name contains apostrophe
 
Hmm, that actually worked, both ways. How about that.

joaquimandrade 06-22-2009 20:42

Re: SQL Error when name contains apostrophe
 
Quote:

Originally Posted by Spunky (Post 854607)
Hmm, that actually worked, both ways. How about that.

Take your time to swallow it


All times are GMT -4. The time now is 15:41.

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