Page 1 of 1
Math - addition question with PHP & mySQL
Posted: Mon Apr 21, 2003 6:22 am
by RT
I'm new to PHP but learning quickly:
My question is I have 3 form values that people select. The values are numbers what I'm trying to do is add for a total the 3 values
Ex:
Plan1 =$249
Optional Services = $15
I need to create the ability to add the two together to create a Total = $264
Do I do this in the mySQL table or add the variables in the PHP code ?
If that's the case how do I do that and can someone point to a link that would explain how to do this ?
Thanks
Posted: Mon Apr 21, 2003 6:31 am
by patrikG
PHP only, the mySQL-database, or databases in general, are usually only used to store data in, while PHP makes all the fancy stuff like calculations etc.
Code: Select all
<?php
$result=$_POST["Plan1"]+$_POST["Optional Services"];
echo "Result: ".$result;
?>
Thanks I'll try it
Posted: Mon Apr 21, 2003 7:20 am
by RT
Thanks for your input I'll give this a try -
One other question how do I insert the results into the Table ?
Posted: Mon Apr 21, 2003 8:22 am
by patrikG
connect to the database with something like this
$connection = @mysql_connect("localhost", "root", "");
$db = mysql_select_db("databasename", $connection) or die ("couldn't connect to database ".mysql_error());
Then use MySQL-statement "INSERT"
$str_sql="INSERT INTO $table (column1, column2,...) VALUES ($value1,$value2)";
$result = mysql_query($str_sql);
Might be worthwhile checking these tutorials - and (later on) what a database-wrapper is :
http://hotwired.lycos.com/webmonkey/pro ... rial4.html
http://www.devshed.com/Server_Side/MySQL/Intro/
http://www.freewebmasterhelp.com/tutorials/phpmysql/
Books on PHP/MySQL I found useful:
MySQL/PHP Database Applications (by Greenspan/Bulger)
PHP - Developer's Cookbook (by Hughes/Zmievski)
Also, always worthwhile checking out
http://www.php.net (the PHP-manual online)
and
http://www.mysql.com/documentation/index.html (the PHP-manual online)
[/url]
Tried it - didn't work
Posted: Mon Apr 21, 2003 1:39 pm
by RT
Tried the code no luck - but I think part of the problem is I didn't explain myself correctly the first time.
I have a form that users select from. What I want to be able to do is have the total show at the botoom of the form - I'm using php to process the form and insert into tables.
<input name="total" type="text" id="total" value="<<?php
$total=$_POST["packplan"]+$_POST["optionservice"];
echo "total ".$result;
?>>
http://www.popwarnernet.net/testphp/order.php
Much easier way of doing this
Posted: Mon Apr 21, 2003 2:20 pm
by Duke of PHP
Seems to me like you are first starting with PHP. I think there is an easier way of approaching this. Let me get this straight...You are making an order form, and want to have the total calculated at the bottom?
I think you right
Posted: Mon Apr 21, 2003 2:29 pm
by RT
In browsing around I think I actually have 2 separate issues here.
One is getting the form to do a calculation within itself.
The other is processing the information via php.
I actually have the php processing part done, its adding the fields in the html form that has me stumped right this second.
Years ago I seem to recall having to use Javascripting to do this but it's been awhile.
Ultimately what I'm trying to do is pass the total results on to Pay Pal for payment.
I suppose what I could do is put a continue order button at that bottom of the form and echo all the field input results to a new page with the total calculation done at that point and then insert the total into the table when they jump to Pay Pal.
Any help greatly appreciated.
Rob
Try tackling it a different way.
Posted: Mon Apr 21, 2003 5:49 pm
by Duke of PHP
Set up the form like this:
<code>
<?
echo "<form action=transaction.php method=post>
<b>Candy Bars:
<br>
QTY: <input type=text name=qty1 size=3>
<input type=submit value='Order'>
</form>
?>
</code>
Instead of using the dropdown menus like you were, or whatever, just use a quantity system. That would make things a lot easier too add together and also insert into the DB.
Reply
Posted: Mon Apr 21, 2003 5:54 pm
by RT
actually when they select an option the value posted is a number what I'm trying to do is add two sets of selected numbers to at some point show the total value of the two options selected.
P.S. AIM = PopWNet if you want to do that as requested earlier.
RT
Posted: Mon Apr 21, 2003 5:59 pm
by Jade
I'd use javascript...its better at doing things on the fly than PHP is...
Jade