搜索功能不工作在php [英] Search function not working in php

查看:147
本文介绍了搜索功能不工作在php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中用户可以为俱乐部注册他们的个人信息,并且它将被存储在数据库中。一切工作正常包括MySQL。
但是,我有搜索功能的问题。搜索页应该是粘性形式。
用户应该能够搜索和显示结果...如果他们在搜索框中键入名字或姓氏,然后结果显示来自数据库的所有信息,满足一定条件
任何人都可以帮助我修复我的搜索功能所有的页面代码如下....
谢谢!!!

I have a form, where user can register their personal information for a club,and it will be stored in the database. Everything is working fine including MySQL. But, I am having problem with the search function. The search page should be sticky form. The user should be able to search and display the result...Like if they type first name or last name in the search box..then the result display all the information from the database,that meets the certain condition Can anyone help me to fix my search function all the page codes are given below.... Thank you!!!

这是我的表单页代码.....

This is my form page code.....

<html>
<head>

<style type="text/css">
table
{
    border-collapse:collapse;
    border-color:#FFFFFF;
}
body
{
    background-color:#39B3A7;
}
</style>

<?php
global $fname,$lname,$age,$gender,$course,$email;

if(!empty($_POST['register']))
{

    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $age=$_POST['age'];
    $gender=$_POST['gender'];
    $course=$_POST['course'];
    $email=$_POST['email']; 

        if (preg_match("/[a-zA-Z ]+$/", $_POST['fname']))  {
            $fname = trim($_POST['fname']);
        }
         else 
        {
        echo '<p>The First name is empty or has illegal characters! To edit please go the link Display Data Information</p>';
        $error = true;
        }


        if (preg_match("/[a-zA-Z ]+$/", $_POST['lname']))  {
            $lname = trim($_POST['lname']);
        }
         else 
        {
        echo '<p>The last name is empty or has illegal characters! To edit please go the link Display Data Information</p>';
        $error = true;
        }


        if(isset($_POST['age']))
        {
            $age=$_POST['age'];
        }
        else
        {
            echo "<p>Please enter your age</p>";
        }


        if(isset($_POST['gender']))
        {   
            $gender = $_POST['gender']; 
        }
        else
        {
            echo "<p>No gender found! So, we assume you are SHEMALE</p>";
        }

        if(isset($_POST['course']))
        {
             $course = $_POST['course'];
        }
        else
        {
            echo "<p>Please Select Course!</p>";

        }

        // Validate the email:
    if (preg_match("/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/", $_POST['email'] )){
    $email = trim($_POST['email']);
    }
    else 
    {
    echo '<p>The email is empty or has illegal characters! To edit please go the link Display Data Information</p>';
    $error = true;
    }
        echo "<br/>";
        echo "<br/>";
        echo "<br/>";



}

    if($fname&&$lname&&$age&&$gender&&$course&&$email)
    {
    require_once('connection.php');
    $query = mysql_query("INSERT INTO members SET FirstName='$fname', LastName='$lname', Gender='$gender', Age='$age',          Email='$email', Course='$course'") or die(mysql_error());

        if($query){
            echo"Your Data Successfully Saved"; 
        }
        else
        {
            echo "Please recheck your Data!";
        }


}


?>
</head>

<body>
<h2><strong>Register Student Account</strong></h2>
<form action="student_form.php" method="post" >
<table border="1">


  <tr>
    <td>First Name</td>
    <td>:</td>
    <td><input type="text" name="fname"  size="30" maxlength="50"/></td>
  </tr>

 <tr>
    <td>Last Name</td>
    <td>:</td>
    <td><input type="text" name="lname" size="30" maxlength="50"/></td>
  </tr>

  <tr>
    <td>Age</td>
    <td>:</td>
    <td><input type="text" name="age"  size="3" /></td>
  </tr>

    <tr>
    <td >Gender </td>
    <td> : </td>
    <td> Male
    <input type="radio" name="gender" value="Male"/>

    Female
    <input type="radio" name="gender" value="Female"/></td>

  </tr>


  <tr>
    <td valign="top">Course</td>
    <td valign="top"> : </td>
    <td> <input type="radio" name="course" value="Bachelor Of Computing"/>Bachelor Of Computing<br/>
   <input type="radio" name="course" value="Bachelor Of Science"/>Bachelor Of Science<br/> 
   <input type="radio" name="course" value="Bachelor Of Software Engineering"/>Bachelor Of Software Engineering<br/>
    <input type="radio" name="course" value="Bachelor Of Networking"/>Bachelor Of Networking<br/>
    <input type="radio" name="course" value="Bacelor Of IT"/>Bacelor Of IT <br/>
    <input type="radio" name="course" value="Bachelor Of Computer Science"/>Bachelor Of Computer Science</td>
  </tr>

 <tr>
    <td>Email Address</td>
    <td>:</td>
    <td><input type="text" name="email"  size="30" maxlength="50"/></td>
</tr>


</table>
    <input type="submit" name="register" value="REGISTER"/>

</form><br>
<p><a href="student_form.php" >Home</a></p>
<p><a href="display_data.php">Display Data Information</a>
<p><a href="search.php">To search for Members</a>
</body>
</html>

这是我的连接页代码....

This is my connection page code....

<?php
$username = "root";
$password = "";

$con = mysql_connect('localhost',$username,$password) or die(mysql_error());

$db=mysql_select_db("taylor");


?>

这是我的display_data代码....

This is my display_data code....

<html>
<head>
<style type="text/css">
table
{
    border-collapse:collapse;
    border-color:#FFFFFF;   
}
body
{
    background-color:#39B3A7;
}


</style>
</head>

<body>

<table border="1">
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
     <th>Email</th>
    <th>Age</th>
    <th>Gender</th>
  <th>Course</th>
    <th>Action</th>
  </tr>
  <?php
  include('connection.php');
    $query=mysql_query("SELECT * FROM members");
    while($rows=mysql_fetch_array($query)){
  ?>

  <tr>
    <td><?php echo $rows['FirstName']; ?></td>
    <td><?php echo $rows['LastName']; ?></td>
     <td><?php echo $rows['Email']; ?></td>
      <td><?php echo $rows['Age']; ?></td>
    <td><?php echo $rows['Gender']; ?></td>


    <td><?php echo $rows['Course']; ?></td>
      <td>
      <a href="edit.php?id=<?php echo $rows['Id']; ?>">Edit</a>
      <a href="delete.php?id=<?php echo $rows['Id']; ?>">Delete</a>
      </td>
  </tr>
  <?php
    }
  ?>
</table>
<p><a href="student_form.php">Home</a></p>
</body>
</html>

这是我的

搜索页代码....

<html>
<head>
<?php
//require_once('student_form.php');
if(isset($_POST['submit'])){ 
$id=$_REQUEST['id']; 
$fname=$_POST['fname'];
    //connect  to the database 
include('connection.php');
//-query  the database table 
$sql="SELECT  * FROM members WHERE FirstName=" . $fname; 
    //-run  the query against the mysql query function 
    $result=mysql_query($sql); 

    while($row=mysql_fetch_array($result)){ 
                $fname=$row['FirstName']; 
                $lname=$row['LastName']; 
                $email=$row['Email'];
                $age =$row['Age'];
                $gender=$row['Gender'];
                $course = $row['Course'];

    //-display  the result of the array 
    echo  "<ul>\n"; 
    echo  "<li>" . $fname . " " . $lname .  "</li>\n"; 
    echo  "<li>" . $email . "</li>\n";
    echo  "<li>" . $age . "</li>\n";
    echo  "<li>" . $gender . "</li>\n";
    echo  "<li>" . $course . "</li>\n"; 

    echo  "</ul>"; 
    } 
} 
?>
</head>
<body>
<form action="search.php" method="post">
<table>
    <tr>
    <td><strong>search box</strong></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="search" value="Enter First Name of The member"size="30"/><input type="submit" value="Search"/></td>

</table>
</form>
</body>
</html>


推荐答案

如果您正在寻找搜索, LIKE 语法,而不是 WHERE

If you are looking to search, you need to use the LIKE syntax, not WHERE

$sql="SELECT  * FROM members WHERE FirstName LIKE '". $fname ."%'";

LIKE& %将其设为通配符。现在你说你想首先和最后搜索,但你只传递你的第一个变量,你只需要查询第一个字段,所以你将需要传递姓氏以及添加一个OR到SQL和封装的名字

The LIKE & % make it a wildcard. Now you said you want to search by first and last, but you only pass in your first variable and you only query the first field, so you will need to pass the last name as well and add an OR to the SQL and encapsulate the firstname like or lastname like in parenthesis for better performance if you expand the sql later..

$sql="SELECT  * FROM members WHERE (FirstName LIKE '". $fname ."%' OR LastName LIKE '". $lname ."%')";

这篇关于搜索功能不工作在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆