Juno Fernando Juno Fernando - 6 months ago 16
MySQL Question

PHP Simple Search Form

I'm new to PHP and would like to start a simple query system by just searching identification number and it will print out full name, date of birth, gender and identification number.

The first file name of the page is search.php.
Inside of the file contains

<html>
<body>

<form action="result.php" method="get">
ID Number: <input type="text" name="idnumber"> <input type="submit" value="Search"><br>
</form>

<p>Input ID number</p>

</body>
</html>


After that, I created a database named users and with following columns (
id, full_name, date_of_birth, gender, identification_number
)

What code should I write into result.php to let the search result of identification number match and found it will print all the details of the person and the search keyword must 100% match.

Example of the scenario.
In database has
John Doe, 20 September 1990, Male, 900920A44
.
Search keyword
"900920A44"
will print out corresponding user's information but if search keyword
"900920A"
will not print out anything because other user might had a
"900920A55"
identification number.

Files:

search.php & result.php

Please advise.

Answer

try this

FORM

<html>
    <body>

    <form action="result.php" method="get">
      ID Number: <input type="text" name="idnumber"> <input type="submit" name="form_submit" value="Search"><br>

   //PUT THE NAME FOR SUBMIT BUTTON 

    </form>

    <p>Input ID number</p>

    </body>
    </html>

result.php

    //database connection

     global $conn;

        $servername = "localhost";  //host name

        $username = "username"; //username

        $password = "password"; //password

        $mysql_database = "dbname"; //database name

        //mysqli prepared statement 

        $conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

       mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");



    if(isset($_GET['form_submit']))
    {

      $IDNUMBER =$_GET['idnumber'];



     $stmt = $conn->prepare("select * from your_table_name_here where identification_number=? ");

                    $stmt->bind_param('s',$IDNUMBER);

                    $stmt->execute();
                $val =  $stmt->get_result();
                $row_count= $val->num_rows;

                if($row_count>0)
                {
                    $result =$val->fetch_assoc();

                    print_r($result);
                }
                else
                {

                  echo "identification_number not Match";
                }



                    $stmt->close();
                     $conn->close();

    }