<?php
include "db.php";
$var = @$_POST['find'];
//check for empty string and display a message.
if($var == ""){
echo "<p>Please input a valid 6-digit ID number</p>";
exit;
} else {
$searchMember = "SELECT * FROM $tablename WHERE id='$var'";
$numresults=mysql_query($searchMember);
$numrows=mysql_num_rows($numresults);
if($numrows == 1){
echo $searchMember['displayname'];
} else if ($numrows == 0){
echo "<p>Sorry, we couldn't find anything that matches your input. Try again?</p>";
}
}
?>
我在 db.php 中包含了连接到数据库的信息。以下是表单的外观:
<body>
<?php
include "db.php";
include "searchSystem.php";
?>
<form name="search" method="post" action="searchSystem.php">
Input a 6-digit ID number: <input type="text" name="find" />
<input type="submit" name="search" value="Search" />
</form>
所以我的表中有一列有 6 位用户 ID。我们想让他们搜索他们朋友的 ID 号码并查看他们的个人资料。
所以这就是我刚刚写的:
<?php
include "db.php";
$var = isset($_POST['find']) ? $_POST['find'] : '';
//check for empty string and display a message.
if($var == ""){
echo "<p>Please input a valid 6-digit ID number</p>";
exit;
} else {
$searchMember = $db->query("SELECT * FROM $tablename WHERE id='$var'");
$row_count = $searchMember->row_count();
$result =$searchMember->fetchALL(PDO::FETCH_ASSOC);
if($row_count == 1){
echo $result['displayName'].' '.$result['id'];
} else if ($row_count == 0){
echo "<p>Sorry</p>";
}
}
?>
还是不行:(