Search function not working in php

2019-06-14 20:09发布

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");


?>

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>

This is my

This is my search page code....

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

4条回答
Rolldiameter
2楼-- · 2019-06-14 20:55
"SELECT  * FROM members WHERE FirstName='" . $fname."'"; 

Your string should be enclosed in a quote when you do your query.

查看更多
等我变得足够好
3楼-- · 2019-06-14 20:55

You can also do this, knowing that The LIKE & % make it a wildcard.

 $sql="SELECT  * FROM members WHERE (FirstName LIKE '". $fname ."%' OR LastName LIKE '". $lname ."%')";
查看更多
迷人小祖宗
4楼-- · 2019-06-14 20:59

Try

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

it should work.

查看更多
对你真心纯属浪费
5楼-- · 2019-06-14 21:09

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

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

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 ."%')";
查看更多
登录 后发表回答