2014-10-19 97 views
0

在我網站的成員頁面上,我需要一個選項,以便成員可以查找其他成員的信息。到目前爲止,我所做的就是創建一個搜索表單...將搜索表單連接到MySQL數據庫並顯示結果

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

我還創建了一個表在我的數據庫與所有成員的信息的。現在,我只需要幫助獲取表單與數據庫表進行通信,並將輸入內容輸入到搜索表單中,並將結果顯示在結果頁上。

我不熟悉這一點,任何幫助將不勝感激。謝謝。

UPDATE: 所以現在我有我的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> 

目前獲得在搜索結果應出現

+0

是的,它的確如此。此錯誤非常常見,並且易於解決以下基本步驟。您的查詢失敗。你需要弄清楚爲什麼。至少有一個答案解釋了這一點,以及如何去做。 – 2014-10-19 16:50:51

+0

我已經實施了您提供的「新」答案頁中的代碼。同樣的錯誤顯示。顯然它不能解決問題。所以介意解釋? – Patrick 2014-10-19 16:52:53

+0

按照[此答案](http://stackoverflow.com/a/11674313/250259)中的步驟對此進行故障排除。對於初學者,您甚至沒有檢查過MySQL是否報告了什麼錯誤。這總是第一步。 – 2014-10-19 16:54:24

回答

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 
} 
} 
?> 
+0

那麼這個PHP代碼會放在我的結果頁面上? – Patrick 2014-10-19 02:02:35

+0

是的。但不要複製和粘貼,你必須把數據庫認證,並讓代碼做的數據。祝你好運! – user3105700 2014-10-19 02:03:39

+0

好的。所以我將搜索表單鏈接到結果頁面。我把上面的代碼放入結果並輸入我的數據庫信息。它什麼都不做。我應該在$和result中的FROM和WHERE中放置什麼?另外,我應該把什麼放在$行?這是什麼需要用戶放入表單並創建搜索? – Patrick 2014-10-19 03:44:30