Jump to content

[SOLVED] how to add to mysql


Crew-Portal

Recommended Posts

Hi I have a system where mebers can rent airplanes, in virtual money and it adds or subtracts a value to mysql. How Do I make it do this? Like I already know how to make it write but I dont want the values to be overwritten I want them to be added! like if the original mysql value was 5 and I wanted to add 6 it would say 11 in the updated mysql table for posting I currently got:

 

elseif ($action == 'postflight'){
if ($_POST['depart'] == NULL){
$error = $tblstart . 'Go Back And Put A Departure!' . $tblend;
include_once($index);
}
elseif ($_POST['arrival'] == NULL){
$error = $tblstart . 'Go Back And Put An Arrival!' . $tblend;
include_once($index);
}
elseif ($_POST['distance'] == NULL){
$error = $tblstart . 'Go Back And Put A Distance!' . $tblend;
include_once($index);
}
elseif (!valid_depart($_POST['depart'])){
$error = $tblstart . 'The Departure You Entered Is Invalid!' . $tblend;
include_once($index);
}
elseif (!valid_arrival($_POST['arrival'])){
$error = $tblstart . 'The Arrival You Entered Is Invalid!' . $tblend;
include_once($index);
}
elseif (!valid_distance($_POST['distance'])){
$error = $tblstart . 'The Distance You Entered Is Invalid!' . $tblend;
include_once($index);
}



else {

$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
$plane = $_POST['plane'];
$time = date("'F j Y, g:i a'");

$strd = $_POST[depart];
$strd = strtoupper($strd);
$stra = $_POST[arrival];
$stra = strtoupper($stra);
$money = round($_POST[distance] + $_POST[distance] / 0.476, 2);
$money2 = '$';
$money_main = '' . $money2 . '' . $money . '';
$sql="INSERT INTO flights (PilotName, FlightDate, AircraftName, NbrPassengers, CargoWeight, DepartureIcaoName, ArrivalIcaoName, TotalDistance, TotalBlockTime, money) VALUES ('CMX-$valid_user', $time, '$_POST[plane]',
 '$passengersrand', '$cargorand Lbs', '$strd', '$stra', '$_POST[distance] NM', '$_POST[hour]:$_POST[minute]', '$money_main')";
 $error = $tblstart . 'Flight Sucessfully Posted!' . $tblend;
 include_once($index);

$result = @mysql_query($sql,$connection) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
}

 

but this code makes a new entry every time... How would I make it so it looked for the logged in user which variable is $valid_user add the inforation into distance like the previous distance stays but the new distance gets added in!

Link to comment
Share on other sites

do a simple query, like this:

 

mysql_query("UPDATE `tablename` SET plane=plane+(5) WHERE id=$valid_user");

 

i hope that helps :)

 

this line is why theres a new entry EVERY time

 

$sql="INSERT INTO flights (PilotName, FlightDate, AircraftName, NbrPassengers, CargoWeight, DepartureIcaoName, ArrivalIcaoName, TotalDistance, TotalBlockTime, money) VALUES ('CMX-$valid_user', $time, '$_POST[plane]',

'$passengersrand', '$cargorand Lbs', '$strd', '$stra', '$_POST[distance] NM', '$_POST[hour]:$_POST[minute]', '$money_main')";

$error = $tblstart . 'Flight Sucessfully Posted!' . $tblend;

include_once($index);

 

it looks like the code is made to store EACH flight, seperately, if you wish to change it, try first checking if the valid user had a row already, and if not, then insert, if he does, t hen used the mysql query i gave you :)

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.