Jump to content

SQL Insert failure


chandlermoss@gmail.com

Recommended Posts

I have a php parser which parses an MPQ file that a user uploads. Once the parse is complete I am trying to INSERT the resulting Dates, Strings, Ints, etc in to a table on my DB. Everything seems to run alright and when I echo the data that I will be inserting, it all seems to output the correct datatype within bounds. With that being the case, each time I parse a file it doesn't seem to get added to the database, whereas when I try and INSERT with a piece of test code using hard-coded variables it works fine. This leads me to believe that some of my data may be wrong but the echo's seem to show otherwise.The following is a generic rundown of the script as it is rather long:

<?php$MAX_FILE_SIZE = 4000000;?><html><head><style type="text/css">table.events {	float: left;}table.events td {	border: solid #000 1px;}table.events th {	border: solid #000 1px;}div.events {	float: left;}</style><script language="JavaScript"><!--function toggleVisible(id) {   var a = document.getElementById?document.getElementById(id):document.all[id];   if (a) {	 if (a.style.display == 'block') a.style.display = 'none';	 else a.style.display = 'block';   }   return false;}//--></script></head><body><?phpinclude $_SERVER['DOCUMENT_ROOT']."/../secure/dbinfo.php";function1(){}function2(){}$con = mysql_connect("localhost",$username,$password);				if (!$con) 				{ 				 echo "NO CONNECTION"; 				 die('Could not connect: ' . mysql_error()); 				} 				$gameName = userFile.value; //fix this later 				echo "GAMENAME = ".$gameName; 				echo "DATEPLAYED = ".$datePlayed; 				echo "Version = ".$version; 				echo "MapName = ".$mapName; 				echo "GameLength = ".$gameLength; 				echo "TeamSize = ".$teamSize; 				echo "RealTeamSize = ".$realTeamSize; 				echo "GameSpeed = ".$gameSpeed; 				echo "Realm = ".$realm; 				echo "Recorder = ".$recorder; 				echo "Player1 = ".$playerOne; 				echo "Player2 = ".$playerTwo; 				echo "Player1Race = ".$playerOneRace; 				echo "Player2Race = ".$playerTwoRace; 				echo "Player1Color = ".$playerOneColor; 				echo "Player2Color = ".$playerTwoColor; 				echo "Player1Team = ".$playerOneTeam; 				echo "Player2Team = ".$playerTwoTeam; 				echo "Player1APM = ".$playerOneAPM; 				echo "PlayerTwoAPM = ".$playerTwoAPM; 				echo "winningPlayer = ".$winningPlayer; 				//$gameNameTwo = $gameName; 				 				$table = $teamSize.'Match';				@mysql_select_db("databaseName", $con) or die("Unable to select DB");				mysql_query("INSERT INTO '$table' (GameName, DatePlayed, Version, MapName, GameLength, TeamSize, RealTeamSize, GameSpeed, Realm, Recorder, Player1, Player2, P1Race, P2Race, P1Color, P2Color, P1Team, P2Team, P1APM, P2APM, Winner, DownloadLink)				VALUES ('$gameName', '$datePlayed', '$version', '$mapName', '$gameLength', '$teamSize', '$realTeamSize', '$gameSpeed', '$realm', '$recorder', '$playerOne', '$playerTwo', '$playerOneRace', '$playerTwoRace', ''$playerOneColor, '$playerTwoColor', '$playerOneTeam', '$playerTwoTeam', '$playerOneAPM', '$playerTwoAPM', '$winningPlayer', 'NOT IMPLEMENTED')");				mysql_close($con);>

The script itself runs fine and the parser outputs the correct data; echo's to see if the connection is established are going off as well, which means I am in fact connecting to the DB, just not inserting. All of the information I wanted protected is in a file above the web root as you can see from the include near the top. I currenty have the database name hard-coded in but will be adding it to the secure file. The following is the testcode I used with hardcoded values for the INSERT:

<?php//echo "made it inside";include $_SERVER['DOCUMENT_ROOT']."/../secure/dbinfo.php";$con = mysql_connect("localhost",$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  }  echo "Connected!";@mysql_select_db("databasename", $con) or die("Unable to select DB");$var = 'TESTINSERT';mysql_query("INSERT INTO 1v1Match (GameName, DatePlayed, Version, MapName, GameLength, TeamSize, RealTeamSize, GameSpeed, Realm, Recorder, Player1, Player2, P1Race, P2Race, P1Color, P2Color, P1Team, P2Team, P1APM, P2APM, Winner, DownloadLink)VALUES ('$var', '1020-01-01 00:00:00', '1.1.3.3245345', 'Xel_naga', '38:59:59', '1v1', '2v1', 'faster', 'US', 'VT|Frank', 'VT|Fizzle', 'VT|Byron', 'Protoss', 'Zerg', 'Green', 'Red', 'Team1', 'Team2', 123, 321, 'VT|Fizzle', 'http://www.downloadhere.com')");mysql_close($con);?>

Any help would be appreciated. At this point I feel I am missing something silly and I can only hope one can see the answer that I am blind to. I have never really learned PHP or MYSQL but have been trying to fumble my way with Google.Thanks again,StackOverflow

Link to comment
Share on other sites

Offhand, I'd look more closely into the parsing routine. For example, one thing you wouldn't catch with a simple echo test is whether you're outputting int values as strings. Whitespace characters could sneak in there too, and an echo would not show them.It's also good practice to check the return value of mysql_query and look for anything in mysql_error

Link to comment
Share on other sites

Offhand, I'd look more closely into the parsing routine. For example, one thing you wouldn't catch with a simple echo test is whether you're outputting int values as strings. Whitespace characters could sneak in there too, and an echo would not show them.It's also good practice to check the return value of mysql_query and look for anything in mysql_error
You are totally right. I checked the output of mysql_error() and got the following:Invalid query: 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 'Naga Caverns', '15 mins, 29 secs', '1v1', '1v1', 'Faster', 'US', 'VTgiX', 'Inflo' at line 2The first thing I noticed was the time. I forgot that I was getting a formatted time in the form of a string instead of an actual TIME type. Secondly, I noticed that the first string 'Nage Caverns' was in the error. This is supposed to be 'Xel'Naga Caverns'. Is the data getting cut because of the apostrophe in the name?Thanks again Deirdre's Dad. Were on the right track!-EDIT: I got it working by defaulting the time to be appropriate and without using a mapName with an apostrophe in it. I tihnk all that is left is for me to make a new table to accept the date(and be more organized) as well as escaping the apostrophe in the mapname.-StackOverflow
Link to comment
Share on other sites

-EDIT: I got it working by defaulting the time to be appropriate and without using a mapName with an apostrophe in it. I tihnk all that is left is for me to make a new table to accept the date(and be more organized) as well as escaping the apostrophe in the mapname.-StackOverflow
In case you haven't found it already, take a look at the mysql_real_escape_string() function to deal with the apostrophe problem. http://uk3.php.net/mysql_real_escape_string
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...