Errant_Shadow Posted January 29, 2009 Share Posted January 29, 2009 I shouldn't be having this problem, I'm not trying to do anything terribly complex, but no matter how hard I try, it's just not working. My hope is a few extra sets of eyes might spot whatever unimaginably simple thing it is that I'm missing :-\ So here goes. Here's what I'm trying to accomplish. I have a flash game that, when the user wins, they can save their game data. The game data consists of 3 variables, myClicks (the number of clicks the user took to win), myTime (the approximate amount of time the user spent playing the round), and timevar (a random number between 0 and 10000 that does nothing but work around an IE cache bug). When the user wins and selects the option to save, the game sends these two variables to a php script called post.php. When this script is loaded, it checks to make sure POST data has been submitted, then it executes a series of if,then conditions that will analyze the user's data and either save or return an error code. Depending on what variables are set, the code will do different things (see below). // first, the script picks up the current session session_start(); // when data is sent from game.swf... // it checks to make sure the variable timevar is present if($_POST['timevar']){ // if it is, I open my database connection for later use include("include/dbc.php"); // test if the user is logged in... if(isset($_SESSION['login'])){ // for debugging purposes, I have it send the variable login back to the game echo '&login=true'; // here I set my query variables so I don't need to escape any data in the string $myName = $_SESSION['name']; $today = $_SESSION['date']; $myIP = $_SERVER['REMOTE_ADDR']; $myClicks = $_POST['myClicks']; $myTime = $_POST['myTime']; // here I query database for the game_id, clicks, play_time, and plays for the given user who played today $query = "SELECT game_id, clicks, play_time, plays FROM games WHERE (player_name='$myName' AND date='$today')"; $result = mysql_query($query); // and assign this data to an associative array $dbarray = mysql_fetch_array($result, MYSQL_ASSOC); // if the query returns results, there is an entry for that user for today. So I modify the existing data if($dbarray){ // here I send the variable dbarray back to the game, just to make sure the code has made it this far with no errors echo '&dbarray=true'; // I also send the user's current plays back to the game // the user is only supposed to be able to save 3 games a day $myPlays = $dbarray['plays']; echo '&test=' . $myPlays; // here's where it fails... if plays is less than 3 if($myPlays < 3){ // set new plsys $newPlays = $dbarray['plays'] + 1; // if $myClicks is greater than the returned clicks, update only plays // analyze specific results after the if, then array if($myClicks>$dbarray['clicks']){ $query = "UPDATE games SET plays='$newPlays'"; $result = mysql_query($query); if($result){ echo '&saved=overwritePlays'; } else { echo '&saved=ERRoverwritePlays'; } // else, if $myClicks is equal to the returned clicks, check $myTime } else if($myClicks==$dbarray['clicks']) { // if new time is less than old time, update entire row if($myTime<$dbarray['play_time']){ $query = "UPDATE games SET clicks='$myClicks', play_time='$myTime', plays='$newPlays'"; $result = mysql_query($query); if($result){ echo '&saved=overwriteTime'; } else { echo '&saved=ERRoverwriteTime'; } } else { echo '&saved=noTime'; } // end if($myTime<$oldTime) // else, if $myClicks is LESS than the returned clicks, update entire row } else if($myClicks<$dbarray['clicks']) { $query = "UPDATE games SET clicks='$myClicks', play_time='$myTime', plays='$newPlays'"; $result = mysql_query($query); if($result){ echo '&saved=overwriteClicks'; } else { echo '&saved=overwriteClicks'; } } // end if($myClicks) } else { // if plays IS 3 // it SHOULD return this when the player has already saved 3 games // but it doesn't. it returns neither, and it gives me "undefined" for the variable "save" echo '&save=noPlays'; } // end if($myPlays) } else { // if no results, enter new data echo '&dbarray=none'; $query = "INSERT INTO games (player_name, date, clicks, play_time, plays, ip_address) VALUES ('$myName', '$today', '$myClicks', '$myTime', '1', '$myIP')"; $result = mysql_query($query); if($result){ echo '&saved=new'; } else { echo '&saved=ERRnew'; } // end if($result) }// end if($bdarray1) } else { echo '&login=false'; } // end if($_SESSION['login'] == true) echo '&EOF=true'; mysql_close(); } // end if($timevar) This script works perfectly when no data is present for the given user for today, and it even works up until the user has saved their 3rd game. But once the user hits 3 plays, it stops working. It SHOULD return the variable save as "noPlays" but it gives me nothing. I've tried reversing the if,then statement, saying if($myPlays > 3) and putting echo '&save=noPlays'; first, with the bulk of the script after the else, but the same thing happens. I've even tried using strict equality and strict inequality, saying if($myPlays == 3) and if($myPlays != 3) I've even tried treating the variable as a string, using strict equality and inequality, saying if($myPlays == "3"), if($myPlays != "3"), if($myPlays == '3'), and if($myPlays != '3'). I've tried using the dbarray['plays'] instead of the variable in all possible configurations, and still it returns "undefined" ... I figured if,then is pretty simple, if the condition is false, then do the other, but it's doing neither of them... what am I doing wrong here? Link to comment https://forums.phpfreaks.com/topic/142926-solved-unusual-difficulty-with-a-simple-script/ Share on other sites More sharing options...
Errant_Shadow Posted January 29, 2009 Author Share Posted January 29, 2009 The thing that's killing me is that it works perfectly up until the user has submitted their 3 games... So it WORKS, until it tries to execute the else statement, then the variables just run off somewhere and never make it to the page. I've looked over and over this script and it looks sound *scratches his head with a sigh* but it's not working, and I need that saved variable to control the end of the game. Link to comment https://forums.phpfreaks.com/topic/142926-solved-unusual-difficulty-with-a-simple-script/#findComment-749524 Share on other sites More sharing options...
printf Posted January 29, 2009 Share Posted January 29, 2009 When is this script run? (IE: before a new game is started or right after a game is finished) Link to comment https://forums.phpfreaks.com/topic/142926-solved-unusual-difficulty-with-a-simple-script/#findComment-749538 Share on other sites More sharing options...
Errant_Shadow Posted January 29, 2009 Author Share Posted January 29, 2009 It's being run after the player wins, when the player chooses to save the game. From Actionscript: on(release){ var result_lv:LoadVars = new LoadVars(); result_lv.onLoad = function(success:Boolean) { if (success) { _root.printLogin = "Login = " + result_lv.login; _root.printResult = "DBResults = " + result_lv.dbarray; _root.printSaved = "Game Saved = " + result_lv.saved; _root.printTest = "Test Variable = " + result_lv.test; } else { trace("Error connecting to server."); } }; var send_lv:LoadVars = new LoadVars(); send_lv.myClicks = _root.myClicks; send_lv.myTime = _root.myTime; send_lv.timevar = Math.round(Math.random()*100000); // IE cache work-around send_lv.sendAndLoad("http://e-kitapci.net/gmStudios/post.php", result_lv, "POST"); } // end on(release) I also just realized that when it updates the game data when a player saves a better game, it's updating every game that player played, so i amended the UPDATE queries with " WHERE game_id='$myGameID'" (of course, after setting $myGameID from the results from my SELECT query)... now they don't update at all... *sighs* Link to comment https://forums.phpfreaks.com/topic/142926-solved-unusual-difficulty-with-a-simple-script/#findComment-749545 Share on other sites More sharing options...
Errant_Shadow Posted January 29, 2009 Author Share Posted January 29, 2009 Looks like after I amended my query to only affect rows with the returned game_id, I needed to take out the parentheses, then use mysql_affected_rows() to verify the data was updated. $query = "UPDATE games SET clicks='$myClicks', play_time='$myTime', plays='$newPlays' WHERE game_id='$myGameID'"; // ... if(mysql_affected_rows() == 1){ echo '&saved=overwriteTime'; } else { echo '&saved=ERRoverwriteTime'; } and it looks like the reason my code was not working on my else statement, was because I misspelled the variable name. Above that, every time I printed my variable, it was "saved=" but at that point I wrote "save=" by mistake. I figured it was some stupid tiny error that I was overlooking... oh well, at least it's solved now Link to comment https://forums.phpfreaks.com/topic/142926-solved-unusual-difficulty-with-a-simple-script/#findComment-750182 Share on other sites More sharing options...
landavia Posted January 29, 2009 Share Posted January 29, 2009 that's not simple like you type on your topic oh well.. plz change this title into SOLVED Link to comment https://forums.phpfreaks.com/topic/142926-solved-unusual-difficulty-with-a-simple-script/#findComment-750186 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.