Hooch Posted May 30, 2006 Share Posted May 30, 2006 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 More sharing options...
reportingsjr Posted May 30, 2006 Share Posted May 30, 2006 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 More sharing options...
Hooch Posted May 30, 2006 Author Share Posted May 30, 2006 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 More sharing options...
Hooch Posted May 30, 2006 Author Share Posted May 30, 2006 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 More sharing options...
Hooch Posted May 30, 2006 Author Share Posted May 30, 2006 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 More sharing options...
reportingsjr Posted May 30, 2006 Share Posted May 30, 2006 well doh.. I was thinking you already pulled all of the columns for mysql.. thats why I did $row['']:)should have said that.. Link to comment Share on other sites More sharing options...
Jack McKalling Posted May 30, 2006 Share Posted May 30, 2006 Does this work completely for you now, Hooch? Link to comment Share on other sites More sharing options...
Hooch Posted May 30, 2006 Author Share Posted May 30, 2006 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 More sharing options...
Jack McKalling Posted May 30, 2006 Share Posted May 30, 2006 What or how exactly do you want to print, I might be able to help you there, or even someone else, it would be faster than a day of searching Link to comment Share on other sites More sharing options...
Hooch Posted May 30, 2006 Author Share Posted May 30, 2006 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 More sharing options...
Jack McKalling Posted May 30, 2006 Share Posted May 30, 2006 (edited) 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 May 30, 2006 by Dan The Prof Link to comment Share on other sites More sharing options...
justsomeguy Posted May 30, 2006 Share Posted May 30, 2006 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 More sharing options...
Jack McKalling Posted May 30, 2006 Share Posted May 30, 2006 I edited my post before due to the quotes Link to comment Share on other sites More sharing options...
Hooch Posted May 31, 2006 Author Share Posted May 31, 2006 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 More sharing options...
justsomeguy Posted May 31, 2006 Share Posted May 31, 2006 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 More sharing options...
Hooch Posted May 31, 2006 Author Share Posted May 31, 2006 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 More sharing options...
Jack McKalling Posted May 31, 2006 Share Posted May 31, 2006 Besides, you would like to give the user the opportunity to choose not to print, or cancel the printing Link to comment Share on other sites More sharing options...
justsomeguy Posted May 31, 2006 Share Posted May 31, 2006 That's the point of showing the box. If you could send something directly to the printer from a web page, it would make email spam look like a drop in the bucket. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now