-4

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

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

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

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

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

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

$db=mysql_select_db("taylor");


?>

这是我的 display_data 代码....

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

4 回答 4

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

当您进行查询时,您的字符串应包含在引号中。

于 2012-06-27T13:18:53.333 回答
3

如果您要搜索,则需要使用LIKE语法,而不是WHERE

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

LIKE & %使其成为通配符。现在您说要按第一个和最后一个进行搜索,但是您只传递了第一个变量并且只查询了第一个字段,因此您还需要传递姓氏并在 SQL 中添加 OR 并封装名字如果稍后扩展 sql,括号中的 like 或 lastname like 可以获得更好的性能。

$sql="SELECT  * FROM members WHERE (FirstName LIKE '". $fname ."%' OR LastName LIKE '". $lname ."%')";
于 2012-06-27T13:23:18.400 回答
1

你也可以这样做,知道 TheLIKE & %使它成为通配符。

 $sql="SELECT  * FROM members WHERE (FirstName LIKE '". $fname ."%' OR LastName LIKE '". $lname ."%')";
于 2014-03-28T21:49:45.450 回答
1

尝试

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

它应该工作。

于 2014-03-26T23:42:11.140 回答