Results 1 to 4 of 4

Thread: Simple PHP If & calculation

  1. #1
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Simple PHP If & calculation

    Hi,

    I have a simple form with radio button and a hidden field.
    Code:
    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:
    Code:
    <?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
    Last edited by mtran; 05-20-2006 at 08:54 PM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Hmmm.
    You never define $type, and you fetched cost from $_POST even though it can't possibly have a value.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Hmmm.
    You never define $type, and you fetched cost from $_POST even though it can't possibly have a value.
    Oops! Thanks!

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •