Php 将radiobutton中的两个变量插入数据库
在从radiobutton插入三个数据之前,我必须显示数据库中的一些数据。三个单选按钮和其他显示数据位于同一位置(以表格形式),如下所示:Php 将radiobutton中的两个变量插入数据库,php,pdo,Php,Pdo,在从radiobutton插入三个数据之前,我必须显示数据库中的一些数据。三个单选按钮和其他显示数据位于同一位置(以表格形式),如下所示: <?php $fetchdata = $db->prepare("SELECT * FROM input_voucher ORDER BY id"); $fetchdata->execute(); $getAllz = $fetchdata->fetchAll(); foreach ($getA
<?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
}
?>
但它节省了“1”,而不是价格。这是什么意思?如何使价格合理?请定义您遇到的问题:出现了哪些错误?脚本是否有任何意外行为? 无论如何,同一组的afaik单选按钮必须具有相同的名称属性,所以所有3个单选按钮的名称都类似于
name=“var”
。另外,$\u POST['var']=
接受单选按钮的value=”“
属性
至于节省价格,您只需为每个单选按钮应用不同的值,并将该值放入db
UPD。关于你的评论
假设ur 3单选按钮包含以下内容:
<input name="var" type="radio" value="0" />1 Month
<input name="var" type="radio" value="1" />2 Month
<input name="var" type="radio" value="2" />3 Month
我终于找到了答案!我还没有为
txtprice1、2和3
声明$\u POST
。这是最后的结果:
$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;
}
}
和您做的一样,只是$var2将$subscription($id)改为$var2,它在一个月内保存为true,但在价格上保存了单词“ARRAY”。
$subscription = array('1 Month', '2 Month', '3 Month');
$price = array($ch1, $ch2, $ch3);
$id = $_POST['var']; #if we selected 1 month, value will be '0'
echo $subscription($id).' for '$price($id); #prints '1 Month for 10$'
$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;
}
}