Search code examples
phpmysqlformsexplodeimplode

Search multiple row separate by comma from mysql using php


It is a tracking system like DHL. Tracking shipment number from MySQL database using php form. but I need it Search multiple row separate by comma from mysql using php. enter image description here

<?php 
      $ship=$_POST['Consignment'];
      $cons = explode(',',$ship);
?>

<?php
    $sql = "SELECT * FROM tbl_courier WHERE cons_no = '$cons[]'";
    $result = dbQuery($sql);
    $no = dbNumRows($result);
    if($no == 1){
    while($data = dbFetchAssoc($result)) {      
    extract($data);
?>

 Shipment Name: <?php echo $ship_name; ?>
 Shipment Phone: <?php echo $phone; ?>

 <?php }//while
        }//if
         else {
         echo 'In else....';
 ?>
    Consignment Number not found.Search Again.

    <?php 
                }//else
    ?>

So I need my search will work with separating by comma(,). Thanks for helping me.


Solution

  • I found the Answer:

    if(isset($_POST['Consignment'])){
    
        $ship=$_POST['Consignment'];
        $shipment= explode(',',$_POST['Consignment']);
    
    $ship = implode("', '",$shipment) ;
    $query = "SELECT * FROM `tbl_courier` WHERE `cons_no` IN('$ship')";
    
    $results = $mysqli->query($query);
    
    if($results){
    print '<table border="1">';
    while($row = $results->fetch_assoc()) {
        print '<tr>';
        print '<td>'.$row["cons_no"].'</td>';
        print '<td>'.$row["customerName"].'</td>';
        print '<td>'.$row["customerPhone"].'</td>';
        print '</tr>';
    }  
    print '</table>';
    
    // Frees the memory associated with a result
    $results->free();
    }
    else {
        echo "Query Not Match";
    }
    $mysqli->close();
    }
    

    Thanks to Answer.