将搜索表单连接到MySQL数据库并显示结果

时间:2014-10-19 01:29:25

标签: php mysql database forms search

在我网站的会员页面上,我需要有一个选项,以便会员可以查看其他会员的信息。到目前为止,我所做的是创建一个搜索表单......

<form id="searchform" name="searchform" action="searchresults.php" method="post">
              Search Membership
              <input type="text" name="search" id="textfield" placeholder="Search Members" />
              <input type="submit" name="button" id="button" value="Search" />
</form>

我还在我的数据库中创建了一个包含所有成员信息的表。现在我只需要帮助获取表单与数据库表进行通信,并将输入的内容输入搜索表单并在结果页面上显示结果。

我不熟悉这一点,任何帮助将不胜感激。感谢。

更新: 所以现在我在searchresults.php页面上有以下PHP代码......

<?php

error_reporting(-1);

$host=""; 
$username="";
$password="";
$db_name="";
$tbl_name="MOAMemberSearch";

mysql_connect($host, $username, $password) or die("cannot connect"); 
mysql_select_db($db_name) or die("cannot select DB");

$sql = "SELECT * FROM MOAMemberSearch";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

if (false === $result) {
    echo mysql_error();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Member Search - MOA</title>

<link href="css/style.css" rel="stylesheet" type="text/css" />

<link href="css/jquery.ennui.contentslider.css" rel="stylesheet" type="text/css" media="screen,projection" />

<style type="text/css">
#content_wrapper #content table {
    color: #000;
}
</style>
</head>

<body>
<div id="menu_wrapper">

    <div id="menu">

        <ul>
            <li><a href="index.html">Home</a></li>
            <li><a href="boardofdirectors.html">Board</a></li>
            <li><a href="members.php" class="current">Members</a></li>
            <li><a href="ratingcard.html">Rating Card</a></li>
            <li><a href="join.html">Join MOA</a></li>
            <li><a href="contact.html">Contact Us</a></li>
        </ul>       

    </div> <!-- end of menu -->
</div> <!-- end of menu wrapper -->

<div id="header_wrapper">
  <div id="header"><!-- end of slider -->
  </div>
  <!-- header -->

</div> 
<!-- end header wrapper -->

<div id="content_wrapper">
    <div id="content">

      <h1>Search Results</h1>
      <table width="930" border="0" style="font-size:12px">
    <tr>
        <th>Sports</th>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Address</th>
        <th>City</th>
        <th>State</th>
        <th>Zip</th>
        <th>Phone 1</th>
        <th>Phone 2</th>
        <th>Email</th>
    </tr>
<tr>
    <td><?php echo $rows['Sports']; ?></td>
    <td><?php echo $rows['LastName']; ?></td>
    <td><?php echo $rows['FirstName']; ?></td>
    <td><?php echo $rows['Address']; ?></td>
    <td><?php echo $rows['City']; ?></td>
    <td><?php echo $rows['State']; ?></td>
    <td><?php echo $rows['Zip']; ?></td>
    <td><?php echo $rows['Phone1']; ?></td>
    <td><?php echo $rows['Phone2']; ?></td>
    <td><?php echo $rows['Email']; ?></td>
</tr>
      </table>

目前在搜索结果应显示的位置出现错误

1 个答案:

答案 0 :(得分:1)

首先,使用mysqli。这将为您节省麻烦并提高安全性。然后,您正在将此类内容视为表单接收页面。

<?php
$con = mysqli_connect(address,user,pass,database);
if(isset($_POST['search'])) {
$result =  mysqli_query($con,"SELECT * FROM members WHERE Name='".mysqli_real_escape_string($con,$_POST['search'])."'");
If(mysqli_num_rows($result)!=0) {
$row = mysqli_fetch_array($result);
//$row is associated array of member data, echo what you want here
}
Else {
//no results
}
}
?>