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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料