Cooper94 Posted December 27, 2008 Share Posted December 27, 2008 I am trying to make it were if lastat equals the icao_orig than do nothing but if lastat dosnt equal icao_orig than - 5 from the money table. I thank you for your time. if ($lastat == $_POST['icao_orig']){ } if ($lastat == $_POST['NULL']){ $money - 5 } else { } Quote Link to comment Share on other sites More sharing options...
revraz Posted December 27, 2008 Share Posted December 27, 2008 IF ($lastat != $icao_orig) { $money = $money - 5; } If they match and you don't want to do anything, then there is no need to check right? Quote Link to comment Share on other sites More sharing options...
Mikedean Posted December 27, 2008 Share Posted December 27, 2008 Edit: My brain is going to pot, Rev is correct Quote Link to comment Share on other sites More sharing options...
Cooper94 Posted December 27, 2008 Author Share Posted December 27, 2008 Ok I have done this but when this happens it changes to -5 in the database. I dont want that I want it to subtract 5 from the exisiting number. I think this code is 100% correct could you clarify? If ($lastat != $icao_orig) { $sub = 5; $money = $user_array['money']; $newmoney = $money - $sub; mysql_query("UPDATE users SET money = '$newmoney' WHERE username = '$username'"); } Quote Link to comment Share on other sites More sharing options...
Cooper94 Posted December 27, 2008 Author Share Posted December 27, 2008 Ok now another problem has come up. Now it just keeps subtracting 5 everytime I file a "pirep". Once again thank you for your time! $pay = $flthrs2 * 40; $money = $user_array['money']; $newacct = $money + $pay; mysql_query("UPDATE users SET money = '$newacct' WHERE username = '$username'"); If ($lastat != $icao_orig) { $sub = 5; $money = $user_array['money']; $newmoney = $money - $sub; mysql_query("UPDATE users SET money = '$newmoney' WHERE username = '$username'"); } Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted December 27, 2008 Share Posted December 27, 2008 hmmm it sounds like your code is constructed in such a manner as to not lend it self to being maintainable with much confidence... I suggest you post the all the code when making requests like this - otherwise the solution offered will fix the portion of code you show but break the portion of code you don't!!! anyhoo lets try and help you a little here... This problem... <?php if ($lastat != $icao_orig) { $sub = 5; $money = $user_array['money']; $newmoney = $money - $sub; mysql_query("UPDATE users SET money = '$newmoney' WHERE username = '$username'"); } ?> the reason that moeny is being set to -5 in the database is becasue $newmoney is being calcualted and the result is -5. Check to see that $user_array has been built properly and contains teh correct info... you can simply up date the record like so... <?php mysql_query("UPDATE users SET money = money - 5 WHERE username = '$username'"); ?> understand that these values may be used elsewhere but doing that kind of update 'protects' you from erroneous code affecting the correct values in the database... one other point here if $lastat is a string then use the strcmp() function - its a little more efficient... next bit... <?php $pay = $flthrs2 * 40; $money = $user_array['money']; $newacct = $money + $pay; mysql_query("UPDATE users SET money = '$newacct' WHERE username = '$username'"); If ($lastat != $icao_orig) { $sub = 5; $money = $user_array['money']; $newmoney = $money - $sub; mysql_query("UPDATE users SET money = '$newmoney' WHERE username = '$username'"); } ?> having two updates like this is a big (and unneccessary) hit on the database... try something like this instead... <?php $newacct = $user_array['money'] + ($flthrs2 * 40); If (strcmp($lastat,$icao_orig) != 0) { $newacct -= 5; } mysql_query("UPDATE users SET money = '$newacct' WHERE username = '$username'"); ?> much less code, much more manageable and only one query regardless of whether you are taking 5 off their cold hard Quote Link to comment Share on other sites More sharing options...
Cooper94 Posted December 27, 2008 Author Share Posted December 27, 2008 Now all its doing is subtracteing 5 from 44 everytime it is filed. I only want it when $icao_orig = $lastat. This is my code full: <?php include 'db.php'; $username = mysql_real_escape_string($_POST['username']); $date = mysql_real_escape_string($_POST['date']); $icao_orig = mysql_real_escape_string($_POST['icao_orig']); $icao_dest = mysql_real_escape_string($_POST['icao_dest']); $aircraft = mysql_real_escape_string($_POST['aircraft']); $routenum = mysql_real_escape_string($_POST['routenum']); $dep_time = mysql_real_escape_string($_POST['dep_time']); $flthrs2 = mysql_real_escape_string($_POST['flthrs2']); $comments = mysql_real_escape_string($_POST['comments']); $hub = mysql_real_escape_string($_POST['hub']); $online = mysql_real_escape_string($_POST['online']); $username = stripslashes($username); $date = stripslashes($date); $icao_orig = stripslashes($icao_orig); $icao_dest = stripslashes($icao_dest); $aircraft = stripslashes($aircraft); $routenum = stripslashes($routenum); $dep_time = stripslashes($dep_time); $flthrs2 = stripslashes($flthrs2); $comments = stripslashes($comments); $hub = stripslashes($hub); $online = stripslashes($online); //check for empty fields if((!$date) || (!$icao_orig) || (!$icao_dest) || (!$routenum) || (!$flthrs2)){ echo 'You did not submit a complete pirep! Please be sure all fields are completed. <br />'; // Show the form again! include 'pirep.php'; exit(); } //Double flight time if an event if ($routenum == 'EVT'){ $flthrs2 = $_POST['flthrs2'] * 2; } else {$flthrs2 == $_POST['flthrs2']; } //insert info into pirep table $comments2 = htmlspecialchars($comments); $sql = mysql_query ("INSERT INTO pirep (date, username, icao_orig, icao_dest, aircraft, routenum, dep_time, flthrs2, hub, online, last_pirep, comments) VALUES('$date','$username','$icao_orig','$icao_dest','$aircraft','$routenum','$dep_time','$flthrs2','$hub','$online','now()','$comments')") or die (mysql_error()); if(!$sql){ echo 'There has been an error processing your pirep. Please contact the webmaster.'; } $sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysql_query($sql); $user_array = mysql_fetch_array($result); //add time to flight hours $flthrs = $user_array['flthrs']; $newflthrs = $flthrs + $flthrs2; mysql_query("UPDATE users SET flthrs = '$newflthrs' WHERE username = '$username'"); mysql_query("UPDATE users SET lastat = '$icao_dest' WHERE username = '$username'"); mysql_query("UPDATE users SET book = '' WHERE username = '$username'"); $newacct = $user_array['money'] + ($flthrs2 * 40); If (strcmp($lastat,$icao_orig) != 0) { $newacct -= 5; } mysql_query("UPDATE users SET money = '$newacct' WHERE username = '$username'"); mysql_close($connection); include 'login_success.php'; echo '<script language="JavaScript"> alert("Flight Approved"); </script>'; ?> Once again I know I am not to good at this and just still learning. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted December 27, 2008 Share Posted December 27, 2008 DOOOOOOOOOOOOOOOODDDD you killing the server!! My bad on the $lastat check - got lost in there somewhere that you only wanted the -5 when they ARE equal which would be <?php if (strcmp($lastat,$icao_orig) == 0).... ?> OK lest look at making you code a bit nicer and easier to play with... <?php include 'db.php'; foreach ($_POST as $key => $val) { $$key = stripslashes(mysql_real_escape_string($val)); } //check for empty fields if ( empty($date) || empty($icao_orig) || empty($icao_dest) || empty($routenum) || empty($flthrs2) ) { echo 'You did not submit a complete pirep! Please be sure all fields are completed. <br />'; // Show the form again! include 'pirep.php'; exit(); } //Double flight time if an event if (strcmp($routenum,'EVT') == 0) { $flthrs2 *= 2; } //insert info into pirep table $comments2 = htmlspecialchars($comments); $sql = mysql_query ("INSERT INTO pirep (date, username, icao_orig, icao_dest, aircraft, routenum, dep_time, flthrs2, hub, online, last_pirep, comments) VALUES('$date','$username','$icao_orig','$icao_dest','$aircraft','$routenum','$dep_time','$flthrs2','$hub','$online','now()','$comments')") or die (mysql_error()); if(!$sql){ echo 'There has been an error processing your pirep. Please contact the webmaster.'; } $sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysql_query($sql); $user_array = mysql_fetch_array($result); //add time to flight hours $flthrs = $user_array['flthrs']; $newflthrs = $flthrs + $flthrs2; $newacct = $user_array['money'] + ($flthrs2 * 40); If (strcmp($lastat,$icao_orig) == 0) { $newacct -= 5; } $sql = mysql_query("UPDATE users SET flthrs = '$newflthrs', lastat = '$icao_dest', book = '', money = '$newacct' WHERE username = '$username'"); mysql_close($connection); include 'login_success.php'; echo '<script language="JavaScript"> alert("Flight Approved"); </script>'; ?> try to make our code more readable - indenting and using new lines will really help - u can always minimize before upload... I hope you can see there are plenty of opportunities to reduce the amount of code you write (reducing code is the goal - you want a minimal amount of code to achieve the task - thats the best solution!) or the number of queries you need to make. Please go back and have a look at why you are escaping all the posted data and stripping slashes when you do - not that thats wrong its just you could save yourself some hassel... Try it and let us know how you get on.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.