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

Issue with MySQL...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Carl Sagan
Senior Member
Join Date: Jan 2014
Old 03-17-2014 , 23:41   Issue with MySQL...
Reply With Quote #1

So I'm having a problem inserting specific data in to a MySQL database.

Here's some of the code:

PHP Code:
new String:g_sReporterName[MAXPLAYERS +1][MAX_NAME_LENGTH];
new 
String:g_sReporterAuth[MAXPLAYERS +1][24];
new 
String:g_sReporteeName[MAXPLAYERS +1][MAX_NAME_LENGTH];
new 
String:g_sReporteeAuth[MAXPLAYERS +1][24];
new 
String:g_sReason[MAXPLAYERS +1][256];
new 
String:g_sServer[MAXPLAYERS +1][256]; 
then when a player selects a menu option it does this which works fine:

PHP Code:
GetClientName(client,g_sReporterName[client],sizeof(g_sReporterName[]));
GetClientAuthString(client,g_sReporterAuth[client],sizeof(g_sReporterAuth[]));
GetClientName(client,g_sReporteeName[client],sizeof(g_sReporteeName[]));
GetClientAuthString(client,g_sReporteeAuth[client],sizeof(g_sReporteeAuth[]));
GetClientName(0,g_sServer[client],sizeof(g_sServer[])); 
and here's where things get messed up. This is in the next menu confirming a report and it tries to insert the info into the database:

PHP Code:
new String:Query[256];
Format(Query,sizeof(Query),"INSERT INTO plugin (reportername,reporterauth,reporteename,reporteeauth,reason,server) VALUES ('%s','%s','%s','%s','%s','%s')",g_sReporterName[client],g_sReporterAuth[client],g_sReporteeName[client],g_sReporteeAuth[client],g_sReason[client],g_sServer[client]);
SQL_FastQuery(Database,Query); 
Now I've checked and the strings have all the correct values, but for some reason it isn't input into the database.

I've tried without the strings and it inserts the data into the database fine, so something has to be wrong there with the strings.

Any ideas?
Carl Sagan is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 03-17-2014 , 23:45   Re: Issue with MySQL...
Reply With Quote #2

Try printing the formatted string. My bet is that 'Query' isn't large enough.

[edit]

Also you should sanitize the player's name using SQL_EscapeString, to prevent problem players from executing a query on your database.

Last edited by bl4nk; 03-17-2014 at 23:47.
bl4nk is offline
Carl Sagan
Senior Member
Join Date: Jan 2014
Old 03-17-2014 , 23:48   Re: Issue with MySQL...
Reply With Quote #3

Quote:
Originally Posted by bl4nk View Post
Try printing the formatted string. My bet is that 'Query' isn't large enough.
I actually tried it at 2560 and still didn't work. Printing it to chat wouldn't work as it gets cut off anyway.
Carl Sagan is offline
pheadxdll
AlliedModders Donor
Join Date: Jun 2008
Old 03-18-2014 , 00:56   Re: Issue with MySQL...
Reply With Quote #4

Print at some offset so you can see the whole string:
Code:
PrintToServer("Query:\n%s", Query[1024]);
Do what bl4nk suggested and use SQL_EscapeString. Since the string isn't escaped, perhaps one of the user provided strings has a character that is blowing up the query. Try executing the printed query manually and see if it goes through. It's probably not a valid sql query.
__________________
pheadxdll is offline
Carl Sagan
Senior Member
Join Date: Jan 2014
Old 03-18-2014 , 07:42   Re: Issue with MySQL...
Reply With Quote #5

Quote:
Originally Posted by pheadxdll View Post
Print at some offset so you can see the whole string:
Code:
PrintToServer("Query:\n%s", Query[1024]);
Do what bl4nk suggested and use SQL_EscapeString. Since the string isn't escaped, perhaps one of the user provided strings has a character that is blowing up the query. Try executing the printed query manually and see if it goes through. It's probably not a valid sql query.
Oh man I feel stupid.

PHP Code:
INSERT INTO plugin (reportername,reporterauth,reporteename,reporteeauth,reason,serverVALUES ('Phil','STEAM_1:0:39841182','Phil','STEAM_1:0:39841182','Rushing','Phil's Test Server') 
Carl Sagan is offline
Carl Sagan
Senior Member
Join Date: Jan 2014
Old 03-18-2014 , 08:10   Re: Issue with MySQL...
Reply With Quote #6

Alright it works now. If anyone's having a similar problem here's how to use SQL_EscapeString:

PHP Code:
new String:Query[512],String:EscapedReporterName[MAX_NAME_LENGTH],String:EscapedReporteeName[MAX_NAME_LENGTH],String:EscapedServer[256],String:EscapedReason[256];
            
SQL_EscapeString(Database,g_sReporterName[client],EscapedReporterName,sizeof(EscapedReporterName));
SQL_EscapeString(Database,g_sReporteeName[client],EscapedReporteeName,sizeof(EscapedReporteeName));
SQL_EscapeString(Database,g_sServer[client],EscapedServer,sizeof(EscapedServer));
SQL_EscapeString(Database,g_sReason[client],EscapedReason,sizeof(EscapedReason));

Format(Query,sizeof(Query),"INSERT INTO plugin (reportername,reporterauth,reporteename,reporteeauth,reason,server) VALUES ('%s','%s','%s','%s','%s','%s')",EscapedReporterName,g_sReporterAuth[client],EscapedReporteeName,g_sReporteeAuth[client],EscapedReason,EscapedServer);
SQL_FastQuery(Database,Query); 
Carl Sagan is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 03-18-2014 , 08:19   Re: Issue with MySQL...
Reply With Quote #7

Some of the strings are way too small. Follow the documentation of "SQL_EscapeString".
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.

Last edited by Dr. Greg House; 03-18-2014 at 08:20.
Dr. Greg House 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 06:22.


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