Jump to content

subtract from a form


Hooch

Recommended Posts

Hey there! I am wanting to get the net wieght of a rail car through a form. I will have the tare wieght in the DB. The gross wieght would be entered in by the user. Now once the gross weight is entered, is it possible to update the database with the net weight with the click of a button? I have no idea how to make the calculation. The following code is what I have so far. I am able to update the database before this net weight issue.

<td align="center"><strong>Area</strong></td><td align="center"><strong>Car ID</strong></td><td align="center"><strong>Tare</strong></td><td align="center"><strong>Gross</strong></td><td align="center"><strong>Net</strong></td><td align="center"><strong>Commodity</strong></td><td align="center"><strong>Onsite</strong></td></tr><tr><td><input name="area" type="text" id="area" value="<? echo $rows['area']; ?>"></td><td><input name="carid" type="text" id="carid" value="<? echo $rows['carid']; ?>"></td><td><input name="tare" type="text" id="tare" value="<? echo $rows['tare']; ?>"></td><td><input name="gross" type="text" id="gross" value=""></td><td><input name="net" type="text" id="net" value="<? WHAT GOES HERE?>"></td><td><input name="commodity" type="text" id="commodity" value="<? echo $rows['commodity']; ?>"></td><td><input name="inout" type="text" id="inout" value="<? echo $rows['inout']; ?>"></td></tr><tr><td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>

I thank you in advance...and I hope it can be done.Hooch

Link to comment
Share on other sites

so, the net weight is the gross-tare? You would do:

$net=$_POST['gross']-$row['tare'];$query="INSERT INTO `table name` ('name of net column') VALUES ('" . $net . "')";$result="mysql_query(query);

BTW, im guessing rows[''] is for the mysql mysql_num_rows() function, its not rows[''].. its row['']no s on the end of row.. Tell me if this is what you were looking for.

Link to comment
Share on other sites

Here's my existing query (above that posted code)

<?phpinclude 'db.php';$sql="SELECT * FROM railcar WHERE id='$id'";$result=mysql_query($sql);$rows=mysql_fetch_array($result);?>

I'm thinking the code you posted needs to be in my form's action.This form is called update.php. Then the action is sent to update_chk.php. I'll have to rethink my structure here. I need to be able to prinout the net weight. So maybe after the update_chk.php has added the info to the database I'll redirect to a page that displays the new calculation made. And just have a print button there. I'll update on my progress. Thanks lad!!Hooch

Link to comment
Share on other sites

I'm getting close. All fields are getting updated, but the net filed is updated with the tare. It's not the calculation result.Here's my update_chk.php

<?phpinclude 'db.php';$net=$_POST['gross']-$row['tare'];$sql="UPDATE railcar SET area='$area', carid='$carid', tare='$tare', gross='$gross', net='$net', commodity='$commodity', inout='$inout' WHERE id='$id'";$result=mysql_query($sql);if($result){echo "Successful";echo "<BR>";echo "HAVE PRINT OUT HERE";}else {echo "ERROR";}/* reportingsjr's help$net=$_POST['gross']-$row['tare'];$query="INSERT INTO `table name` ('name of net column') VALUES ('" . $net . "')";$result="mysql_query(query);*/?>

When I add the VALUES ('" . $net . "')"; it causes errors. I know you didn't mean for it to go where I am putting it now. Would you mind having a boo?

Link to comment
Share on other sites

Ah Ha! I changed$net=$_POST['gross']-$row['tare'];to$net=$_POST['gross']-$_POST['tare'];and it worked!! Even though the tare is in the DB I figured it still is getting posted. So what the hay.

Link to comment
Share on other sites

I think so. Everything is working so far. I even was able to redirect to a page that shows the recently updated info. Now off to google to see how to print my results!! Thank you everyone!!

Link to comment
Share on other sites

Sure thing man. The end of the script there's a table. Everything inside it...

<?phpinclude 'db.php';	$net=$_POST['gross']-$_POST['tare'];$sql="UPDATE railcar SET area='$area', carid='$carid', tare='$tare', gross='$gross', net='$net', commodity='$commodity', inout='$inout' WHERE id='$id'";$result=mysql_query($sql);if($result){include 'db.php';$today0 = date('l dS \of F Y h:i:s A');// Prints something like: Monday 15th of August 2005 03:12:46 PM $result = mysql_query("SELECT * FROM railcar WHERE id='$id';");while($row=mysql_fetch_array($result)){?><table width="300" border="0" cellspacing="0" cellpadding="0">  <tr>    <td colspan="4"><? echo $today0; ?></td>  </tr>  <tr>    <td height="40" colspan="4" valign="bottom"><div align="left"><? echo $row['carid'];?></div></td>  </tr>  <tr>    <td width="51">Tare</td>    <td width="67"><div align="right"><? echo $row['tare'];?></div></td>    <td width="47"> kg's</td>    <td width="135"> </td>  </tr>  <tr>    <td>Gross</td>    <td><div align="right"><? echo $row['gross'];?></div></td>    <td> kg's</td>    <td> </td>  </tr>  <tr>    <td>Net</td>    <td><div align="right"><? echo $row['net'];?></div></td>    <td> kg's</td>    <td> </td>  </tr>  <tr>    <td><a href="list_records.php">Update</a></td>    <td><a href="index.php">Home</a></td>    <td> </td>    <td> </td>  </tr></table><?}}else {echo "ERROR";}?>

Pretty much the if($result){ success part. Thanks Dan!

Link to comment
Share on other sites

I don't see any problem with this, doesn't it work? :)Else, I suggest replacing the while construction by a similar construction, that ensures you to be executed only once, while with the while it would be executed more than once if the $id contains more rows :) Like this:

$row = mysql_fetch_array($result);if ($row){...}
Next, you can remain in PHP mode when outputting the table, by replacing that php-end tag by:
echo "
and the php-begin tag (after the table) by:
";
But then don't forget to escape the already residing double quotes, and to take care of the php tags, inside the table code.[*Edit:]Your way is easier, but with my way there are no needs for php tags, comes in handy when there are many of them. Edited by Dan The Prof
Link to comment
Share on other sites

Next, you can remain in PHP mode when outputting the table, by replacing that php-end tag by: echo "and the php-begin tag (after the table) by: ";

That won't work for this example because of the quotes, he would need to escape all quotes also. But you can use heredoc syntax instead to print the whole thing.You can replace this:
<?php...$result = mysql_query("SELECT * FROM railcar WHERE id='$id';");while($row=mysql_fetch_array($result)){?><table width="300" border="0" cellspacing="0" cellpadding="0"> <tr>   <td colspan="4"><? echo $today0; ?></td> </tr>huge html chunk here</table><?}?>

With this:

<?php...$result = mysql_query("SELECT * FROM railcar WHERE id='$id';");while($row=mysql_fetch_array($result)){$html = <<<END_OF_HTML<table width="300" border="0" cellspacing="0" cellpadding="0"> <tr>   <td colspan="4">{$today0}</td> </tr>huge html chunk here (including PHP variables if you want, no need to escape quotes)</table>END_OF_HTML;echo $html;}?>

More info on heredoc syntax.

Link to comment
Share on other sites

I don't see any problem with this, doesn't it work?
It displays the info perfectly. When I say print..I mean actually print out the data on a printer. I was hoping to have a button on that "if($result){ success part"So a simple click could print my data out. And thank you very much for your input guys.
Link to comment
Share on other sites

You can do one of these jobs:<a href="java script: void(0);" onclick="window.print();">send it to the printar!</a>But you can't send the document directly to the printer automatically, it has to show the print box still. Try it out.

Link to comment
Share on other sites

You can do one of these jobs:<a href="java script: void(0);" onclick="window.print();">send it to the printar!</a>But you can't send the document directly to the printer automatically, it has to show the print box still. Try it out.
Cha ching! Thank you.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...