PHP简单搜索表单

时间:2016-05-30 06:36:44

标签: php html mysql

我是PHP的新手,想通过搜索身份证号码来启动一个简单的查询系统,它会打印出全名,出生日期,性别和身份证号码。

页面的第一个文件名是 search.php 。 文件内部包含

<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>

之后,我创建了一个名为users的数据库,其中包含以下列(id, full_name, date_of_birth, gender, identification_number

我应该在result.php中写入什么代码,让识别号码的搜索结果匹配,并发现它将打印该人的所有详细信息,并且搜索关键字必须100%匹配。

场景示例。 在数据库中有John Doe, 20 September 1990, Male, 900920A44。 搜索关键字"900920A44"会打印出相应的用户信息,但如果搜索关键字"900920A"不会打印出任何内容,因为其他用户可能会有"900920A55"标识号。

文件:

search.php&amp; result.php

请告知。

2 个答案:

答案 0 :(得分:1)

试试这个

<强> 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();

    }

答案 1 :(得分:0)

使用此功能。

login.php

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

result.php

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "users";
mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database) or die("Could not select database");
 $idmark = $_GET['idnumber'];
$sql = "select * from student where identification_number = '".$idmark."'";
$result = mysql_query($sql);?>
<table width="70%" border="1px solid black">
            <tr style="background-color:green; color:white">
                <th><strong>ID</strong></th>
                <th><strong>Name</strong></th>
                <th><strong>DOB</strong></th>
                <th><strong>GENDER</strong></th>
                <th><strong>Identification No</strong></th>

            </tr>
            <?php if (mysql_num_rows($result)>0) {  $i=0; 
            while($row = mysql_fetch_assoc($result)) { ?>
      <tr class="<?php echo ($i%2) ? 'even' : 'odd' ?>">
                <td><?php echo $row['id'];?></td>
                <td><?php echo $row['full_name'];?></td>
                <td><?php echo $row['date_of_birth'];?></td>
                <td><?php echo $row['gender'];?></td>
                <td><?php echo $row['identification_number'];?></td>

            </tr>


             <?php $i++;}}
?>