Jump to content

Help Please


Cooper94

Recommended Posts

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'");

}

Link to comment
Share on other sites

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'");
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

DOOOOOOOOOOOOOOOODDDD

 

you killing the server!! :P

 

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....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.