我有两个文本框。
收据编号,优惠券编号
用户以逗号分解格式在收据文本框中输入值。
然后单击“搜索”,然后根据优惠券文本框中输入的收据获取所有值(value)。
我的数据在表中,例如
收据优惠券
501 - 1
501 - 2
501 - 3
502 - 4
502 - 5
502 - 6
对于前任 -
现在,如果用户在receipttextbox中输入501,502并单击搜索,则会在coupontextbox中显示501,502的所有优惠券
点击搜索后,我需要在优惠券文本框中输出如下所示的内容
优惠券文本框 = 1,2,3,4,5,6
请建议如何从收据文本框中获取逗号分解输入并放入搜索查询
下面是我的代码
<?php
$coupons = "";
$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();
$coupons = array();
foreach($data as $row)
{
$coupons[] = $row['coupon'];
}
}
?>
优惠券文本框
<input type="text" name="coupon" readonly="true" value="<?php echo implode(',', $coupons); ?>" class="field size2" />
最佳答案
如果将explode() $_POST['receipt_no']
转换为数组,则可以将其与 IN 运算符一起使用。请参阅PDO info关于如何将 IN 与 PDO 结合使用。
下面的代码使用了它。
if($_POST && isset($_POST['submit']))
{
$arr = explode(',', $_POST['receipt_no']);
$in = str_repeat('?,', count($arr) - 1) . '?';
$sql = "SELECT coupon FROM scheme_master WHERE receipt_no IN ($in)";
$result=$db->prepare($sql);
$result->execute($arr);
$data = $result->fetchAll();
$coupons = array();
foreach($data as $row)
{
$coupons[] = $row['coupon'];
}
}
关于php pdo代码搜索逗号分解输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28539887/