Jump to content

Php Search MySql


NasimUddinAhmmad

Recommended Posts

Hello friends,

I am a novice in php. Though I create 2 scripts:

 

1. students registration form

2. search students by Registration Number

 

Our students' Registration Numbers are as follows: nifeA001, nifeA002 & so on...

 

But when someone enter just "nife" or "n" or "A" etc all the students's data is showing.

 

I want that students can search data by entering only Full Registration Number not a Part.

 

I am placing the search.php code below. Please help me out....

 

----------------- Search.php ------------------

<?php

mysql_connect("localhost", "root", "") or die("could not connect");

mysql_select_db("student") or die("could not connect");

 

//collect

if(isset($_POST['search'])) {

    $searchq = $_POST['search'];

    $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);

 

//here

$query = mysql_query("SELECT * FROM user WHERE registration LIKE '%$searchq%'") or die("could not search!");

    $count = mysql_num_rows($query);

    if($count == 0){

        $output = 'There was no search results!';

    }else{

        while($row = mysql_fetch_array($query)) {

            $fname = $row['firstname'];

            $lname = $row['surname'];

            $id = $row['registration'];

 

            $output .= '<div> '.$id.' '.$fname.' '.$lname.'</div>';

               }

        }

}

 

?>

 

<html>

<head>

</head>

<body>

<form action="form.php" method="post">

    <input type="text" name="search" placeholder="search for students.."

    <input type="submit" value=">>" />

</form>

 

<?php print("$output");?>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/293892-php-search-mysql/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.