Malik Ali Jamil Malik Ali Jamil - 2 months ago 14
MySQL Question

how to search multiple ids in a html search form to get mysql rows with php?? (random and sequential)

please i need your help ..

i can view a single person result by filling an html form selecting board as an option and search by roll number (123456) to view all columns...

with below php

<?php

// php code to search data in mysql database and set it in input text

if(isset($_POST['search']))
{
// id to search
$roll = $_POST['roll'];
$board = $_POST['board'];

//connect to the database
include('connect.php');

// mysql search query
$query = "SELECT `firstname`, `lastname`, `roll`, `city`, `board` FROM `result` WHERE `roll` = $roll and `board` = $board LIMIT 1";
$result = mysqli_query($connect, $query);

// if id exist
// show data in inputs
if(mysqli_num_rows($result) > 0)
{

echo "<table width='540' height='69'><thead><tr>
<th width='14%'><div align='center'>first name</div></th>
<th width='12%'><div align='center'>last name</div></th>
<th width='17%'><div align='center'>roll number</div></th>
<th width='14%'><div align='center'>city</div></th>
<th width='22%'><div align='center'>board</div></th>
</tr></thead>
<tfoot>
</tfoot>
<tbody>
<tr class='alt'>";

while ($row = mysqli_fetch_array($result))
{
echo "<td><div align='center'>" . $row['firstname'] . "</div></td>";
echo "<td><div align='center'>" . $row['lastname'] . "</div></td>";
echo "<td><div align='center'>" . $row['roll'] . "</div></td>";
echo "<td><div align='center'>" . $row['city'] . "</div></td>";
echo "<td><div align='center'>" . $row['board'] . "</div></td>";
}
echo "</tbody>";
echo "</table>";



}


mysqli_free_result($result);
mysqli_close($connect);

}

?>


now what i want is to search for random roll number ..

please view the form image

enter image description here

kindly make changes in my php so i can use this form with it..

random result should be like this

firstname lastname roll city board
name1 lname1 3456 - -
name2 lname2 8765 - -
name3 lname3 4567 - -


also sequential e.g write (from 1001 to 1100) in search to view result of 100 people..

sequential result should be like this

firstname lastname roll city board
name1 lname1 1001 - -
name2 lname2 1002 - -
name3 lname3 1003 - -
.............................................
name100 lname100 1100 - -

Answer

You need to change your query according to your need

 $query = "SELECT `firstname`, `lastname`, `roll`, `city`, `board` 
FROM `result` WHERE 1=1 ";

if(isset( $_POST['roll'])){ // Assuming this as single roll text
   $query.=" AND `roll` = '$roll'";
 }
if(isset($_POST['fromroll']) && isset($_POST['toroll'])){ // This as From and To
  $fromroll=$_POST['fromroll'];
  $toroll=$_POST['toroll'];
$query.=" AND roll between '$fromroll' AND '$toroll' ";
 }
if(isset($_POST['multipleroll']))
{
  $multipleroll=$_POST['multipleroll'];
 $query.=" AND roll in ('$multipleroll') "; 
}

$result = mysqli_query($connect, $query);