PHP搜索结果和链接照片到profile.php页面?

时间:2014-04-08 04:15:22

标签: php mysql

如何创建指向这些搜索结果的每张照片或文本的链接到profile.php来查询和输出数据库?

简而言之 - 搜索数据库并单击任何可以带您进入其个人资料页面的结果。示例:Match.com,facebook.com ...等(搜索并单击以查看配置文件)。

另外,如何在同一行输出名字和姓氏?

请帮助。

这是HTML搜索表单。

<h2>Search</h2> 
<form name="search" action="searchresults.php" method="POST">
Search for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="firstName">First Name</option>
<Option VALUE="email">Email</option>
</Select>


Search for: <input type="text" name="find1" /> in 
<Select NAME="field1">
<Option VALUE="lastName">Last Name</option>
</Select>

<br><br>

Search for: <input type="text" name="find2" /> in 
<Select NAME="field2">
<Option VALUE="gender">Gender</option>
</Select>

<br><br>

Search for: <input type="text" name="find3" /> in 
<Select NAME="field3">
<Option VALUE="age">Age</option>
</Select>

<br><br>

Search for: <input type="text" name="find4" /> in 
<Select NAME="field4">
<Option VALUE="city">City</option>
</Select>

Search for: <input type="text" name="find5" /> in 
<Select NAME="field5">
<Option VALUE="state">State</option>
</Select>


<br><br>

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

这是searchresults.php。

DATABASE CONNECTION

$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

$find=$_POST['find'];
$field=$_POST['field'];

$find1=$_POST['find1'];
$field1=$_POST['field1'];

$find2=$_POST['find2'];
$field2=$_POST['field2'];

$find3=$_POST['find3'];
$field3=$_POST['field3'];

$find4=$_POST['find4'];
$field4=$_POST['field4'];

$find5=$_POST['find5'];
$field5=$_POST['field5'];

$data="SELECT firstName, lastName, email, userphoto, gender, age, city, state FROM    actorsInfo 
WHERE upper($field) LIKE '%$find%'
AND upper($field1) LIKE '%$find1%'
AND upper($field2) LIKE '%$find2%'
AND upper($field3) LIKE '%$find3%'
AND upper($field4) LIKE '%$find4%'
AND upper($field5) LIKE '%$find5%'
"; 

$result = mysql_query($data);
$count=mysql_numrows($result);

echo '<br><br>';

if($count > 0){
echo"<table border=0>";

//get images and names in two arrays

$firstName= $row["firstName"];
$lastName= $row["lastName"];
$email= $row["email"]; 
$userphoto= $row["userphoto"];
$gender= $row["gender"];
$age= $row["age"];
$city= $row["city"];
$state= $row["state"];

$age = array();
$gender = array();
$userphoto = array();
$firstName = array();
$lastName = array();
$city = array();
$state = array();

while ($row = mysql_fetch_array($result))
{
$userphoto[] = "<img src='images/".$row['userphoto']."' height='200' width='160'>";
$firstName[] = $row['firstName'];
$lastName[] = $row['lastName'];
$age[] = $row['age'];
$gender[] = $row['gender'];
$email[] = $row['email'];
$city[] = $row['city'];
$state[] = $row['state'];

}

while(!empty($userphoto))
{       
//output images     
foreach(array($userphoto, $firstName, $lastName, $age, $email, $city, $state) as $items)
{
    echo "<tr>";

    foreach($items as $key=>$item)
    {
        echo "<td><font size =\"2\" >$item</td>"; 

        //output only four of them
        if($key==4)
        {
            break;
        }
    }
    echo "</tr>";
}
//remove the first five images from $images because they're already printed
$userphoto = array_slice($userphoto, 5);
$firstName = array_slice($firstName, 5);
$lastName= array_slice($lastName, 5);
$email = array_slice($email, 5);
$age = array_slice($age, 5);
$city = array_slice($city, 5);
$state = array_slice($state, 5);

}

1 个答案:

答案 0 :(得分:0)

以下是将id传递给profile.php。

的解决方案
$userphoto[] = "<a href='profile.php?id=".$row['id']."'><img   src='images/".$row['userphoto']."' height='200' width='160'></a>";

这是profile.php。

<?php
$id = (int)$_GET['id'];
if (isset($_GET['id']))
{
//fetch and display the information with database Query 

$con=mysqli_connect("127.0.0.1", "admin", "password","actors");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM actorsInfo where id = " . $_GET['id']);

while($row = mysqli_fetch_array($result))
{


echo $row['firstName'] . " " . $row['email'];
echo "<br>";
}
mysqli_close($con);
}

?>