Log in

View Full Version : Simple PHP If & calculation



mtran
05-20-2006, 07:13 PM
Hi,

I have a simple form with radio button and a hidden field.


Item:
<form action="process.php" method="post">
<input name="item" type="radio" value="DVD"> DVD
<input name="item" type="radio" value="VHS">VHS
Quantity:
<input name="quantity" type="text">
<input name="cost" type="hidden">
</form>


I want to post the info to db and do simple calculation at the same time, but it doesn't work.

In process.php, I have:


<?php
$item = $_POST['item'];
$quantity =$_POST['quantity'];
$cost = $_POST['cost'];

if($item=="DVD")
$cost=$quantity*2;
else $cost=$quantity*1;

mysql_query ("INSERT INTO list (item,quantity,cost)
VALUES('$item','$quantity','$cost')",$connection);
?>


The item[DVD or VHS] and quantity are posted OK to db, but the cost is 0. What's wrong?
Thanks

Twey
05-20-2006, 07:47 PM
Hmmm.
You never define $type, and you fetched cost from $_POST even though it can't possibly have a value.

mtran
05-20-2006, 09:12 PM
Hmmm.
You never define $type, and you fetched cost from $_POST even though it can't possibly have a value.

Oops! Thanks!

Twey
05-20-2006, 09:23 PM
What the heck am I talking about?
Sorry, I'm not thinking very well today. There is no $type, and $_POST['cost'], while there's no need to fetch it, shouldn't cause any harm if you do, since you don't use it anywhere.