doucong1853 2014-12-27 23:31
浏览 67
已采纳

将radiobutton中的两个变量插入数据库

Before Insert Three data from radiobutton, I have to display some data from database. three radiobuttons and other display data is the same place (in a form), as follows:

    <?php
    $fetchdata = $db->prepare("SELECT * FROM input_voucher ORDER BY id");
    $fetchdata->execute();
    $getAllz = $fetchdata->fetchAll();
    foreach ($getAllz as $rowz) {
      $ch1=$rowz['price1'];
      $ch2=$rowz['price2'];
      $ch3=$rowz['price3'];
    ?>

<form action="" method="post" name="form">
    <input name="var" id="voucher1" type="radio" value="1 Month">1 Month
    <span>USD.<input type="text" name="txtprice1" id="txtprice1" value="<?php echo $ch1; ?>" readonly="readonly"></span>

    <br />

    <input name="var" id="voucher2" type="radio" value="2 Month">2 Month
    <span>USD.<input type="text" name="txtprice2" id="txtprice2" value="<?php echo $ch2; ?>" readonly="readonly"></span>

    <br />

    <input name="var" id="voucher3" type="radio" value="3 Month">3 Month
    <span>USD.<input type="text" name="txtprice3" id="txtprice3" value="<?php echo $ch3; ?>" readonly="readonly"></span>

    <br />
    <input type="submit" class="btn btn-primary" name="btnsubmit" id="btnsubmit" value="Contiue">
    </form>

    <?php
    }
    ?>

Then, User must choose ONE CHOICE from the tree radiobuttons. When user check one of the buttons that means user also choose type of prices (price1,price2 or price3). The result from the choice picked is inserted into database, as follows:

UPDATED:

if (isset($_POST['btnsubmit'])){
   $var= strip_tags($_POST['var']);
   if ($var=='1 Month'){
      $numprice=strip_tags(isset($_POST['txtprice1']));
      }
   elseif ($var=='2 Month'){
      $numprice=strip_tags(isset($_POST['txtprice2']));
      }
   elseif ($var=='3 Month'){
      $numprice=strip_tags(isset($_POST['txtprice3']));
      }
      //insert now
      $mydata = $db->prepare("INSERT INTO voucher (colmonth,price) VALUES (:var,:numprice)");
      $mydata->execute(array(':var'=>$var,':numprice'=>numprice));
      echo "successfully saved!";
      header('location:setup.php');
      exit();
   }

But it saved "1", not price. What does it mean? How to make it right price?

  • 写回答

2条回答 默认 最新

  • dqsw7529 2014-12-28 07:20
    关注

    finally I found the answer! I haven't declared yet $_POST for txtprice1,2 and 3. Here's the final:

    $txtprice1 = strip_tags(isset($_POST['txtprice1'])) ? strip_tags($_POST['txtprice1']) : '';
    $txtprice1=strip_tags($txtprice1);
    $txtprice2 = strip_tags(isset($_POST['txtprice2'])) ? strip_tags($_POST['txtprice2']) : '';
    $txtprice2=strip_tags($txtprice2);
    $txtprice3 = strip_tags(isset($_POST['txtprice3'])) ? strip_tags($_POST['txtprice3']) : '';
    $txtprice3=strip_tags($txtprice3);
    $var = strip_tags($_POST['var']);
    if ($var=='1 Month'){
        if (isset($_POST['txtprice1'])){
            $numprice=$txtprice1;
            }
        }
    elseif ($var=='2 Month'){
        if (isset($_POST['txtprice2'])){
            $numprice=$txtprice2;
        }
    }
    elseif ($var=='3 Month'){
        if (isset($_POST['txtprice3'])){
            $numprice=$txtprice3;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)