php pdo代码在一个文本框中回显多个值

时间:2015-02-09 10:43:07

标签: php pdo

字段名称 - 收据号,优惠券,schemename

我创建了一个没有收据文本框的表单。

需要根据输入的收据搜索所有优惠券号码。并且需要在文本框中以逗号分隔格式回显所有优惠券。

for ex-- in my database ... foll。数据存在..

收到没有优惠券schemename

701 511一个月

701 512一个月

701 513一个月

现在我需要选择701的所有优惠券并在优惠券文本框内回显。

for ex - 511,512,513 echo all couponno in one text。

PLZ帮我怎么做

<?php
$scheme = "";$book = "";$coup = "";$amou = "";$rec = "";$cit = "";
 $db=new PDO('mysql:host=localhost;dbname=circulation_scheme_prepaid','root','');
if($_POST && isset($_POST['submit']))
{
    $result=$db->prepare('SELECT scheme_name,coupon, FROM scheme_master WHERE receipt_no=:receipt_no');
    $result->bindParam(':receipt_no',$_POST['receipt_no']);
    $result->execute();
    foreach($result as $row){    
        $scheme =  $row['scheme_name'];         
        $coup =  $row['coupon'];  
    }
}?>

**receipt no textbox** - I get input from this textbox
<input type="text" name="receipt_no"  /> 

我需要在文本框下面回复所有优惠券。

优惠券文本框 - <input type="text" name="coupon" value="<?php echo $coup;?>" class="field size2" />

2 个答案:

答案 0 :(得分:1)

这应该有效。

<?php
$scheme = "";$book = "";$coup = "";$amou = "";$rec = "";$cit = "";
 $db=new PDO('mysql:host=localhost;dbname=circulation_scheme_prepaid','root','');
if($_POST && isset($_POST['submit']))
{
    $result=$db->prepare('SELECT scheme_name,coupon, FROM scheme_master WHERE receipt_no=:receipt_no');
    $result->bindParam(':receipt_no',$_POST['receipt_no']);
    $result->execute();
    $data = $result->fetchAll();
    $coupons = array();

    foreach($data as $row){    
        $coupons[] = $row['coupon']; 
    }
}?>

<input type="text"  value="<?php echo implode(',', $coupons); ?>"/> 

答案 1 :(得分:0)

<?php
$scheme = "";$book = "";$coup = "";$amou = "";$rec = "";$cit = "";
$db=new     PDO('mysql:host=localhost;dbname=circulation_scheme_prepaid','root','');
if($_POST && isset($_POST['submit']))
{
    $result=$db->prepare('SELECT coupon FROM scheme_master WHERE receipt_no=:receipt_no');
    $result->bindParam(':receipt_no',$_POST['receipt_no']);
    $result->execute();
    $data = $result->fetchAll();

    foreach($data as $row) {    
       $value[] = end($row); 
     }
     $coupons = implode(',', $value);
}?>


<input type="text"  value="<?php echo $coupons ?>"/>