如何在单个查询中获取多个ID的记录

时间:2019-01-11 13:47:37

标签: php mysql forms

这是我的桌子。单击“完成”时,它会选中已选中的复选框ID,并在文本框中显示。在PDF按钮上,单击它将文本框输入发布到另一页:

enter image description here

所以我想获取所有ID的详细信息,但是我的代码仅显示第一个ID的结果

if (isset($_POST['allids'])) {
 $query = "SELECT * FROM tbl_order
 WHERE order_id = '".$_POST['allids']."'";
 $result = mysqli_query($conn, $query);

 while($row = mysqli_fetch_array($result))
 {
  echo $row['order_no']."</br>";
  echo $row['ordercustomer_name']."</br>";
 }

}

这是我的表格

<form method="POST" action="fetch3.php" target="_blank">
 <div class="container" id="order_data">

</div>

<div>
  <h4><strong>GENERATE REPORT</strong></h4>      
  <input type="text" name="allids" id="allids">
  <button type="button" class="btn btn-primary btn-lg btn_done" name="btn_done" id="btn_done"><i class='fa'></i> DONE</button>     
  <button type="submit" class="btn btn-warning btn-lg btn_pdf" name="btn_pdf" id="btn_pdf"><i class='fa fa-file-pdf-o'></i> PDF</button>     
</div>
</form>

3 个答案:

答案 0 :(得分:2)

您将在SQL查询中使用Set<Item> itemSet = new HashSet<>(); Map<Item,Integer> occurencesMap = new HashMap<>(); itemSet.add(yourItem); itemSet.add(yourItem); itemSet.add(secondItem); //itemSet now contains only 2 objects! if (occurencesMap.get(yourItem) == null){ occurencesMap.put(yourItem,1); } else{ //adding 1 to the current value of occurences for that item. occurencesMap.get(yourItem)++; } 语句,并用逗号分隔ID组:

IN

警告:Little Bobby your script is at risk for SQL Injection Attacks. 。甚至escaping the string都不安全!

了解有关preparedMySQLi语句。专为WHERE order_id IN (695,392) 使用these instructions

答案 1 :(得分:-2)

您可以使用:

if (isset($_POST['allids'])) {
 $query = "SELECT * FROM tbl_order
 WHERE order_id in('".$_POST['allids']."')";
 $result = mysqli_query($conn, $query);

 while($row = mysqli_fetch_array($result))
 {
  echo $row['order_no']."</br>";
  echo $row['ordercustomer_name']."</br>";
 }

}

答案 2 :(得分:-2)

现在一切正常,谢谢大家

$id = $_POST['allids'];

$query = "SELECT * FROM tbl_order
WHERE order_id IN($id)";
$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_array($result))
{
 echo $row['order_id']."</br>";
 echo $row['order_no']."</br>";
 echo $row['ordercustomer_name']."</br>";
}