使用PHP将搜索结果链接到MySQL的项目

时间:2014-08-11 09:00:31

标签: php mysql search hyperlink

我有两个文件返回错误," search.php"和" advert.php?= $ id"。我需要将我的结果与超链接相关联,该超链接将用户发送到特定的"广告页面"。结果将是一组广告,有谁知道我可能出错的地方,如果可能的话,如何修复错误?他们似乎对我好。

由于

的search.php

    <?php

    include("connect.php");

    //if user didn't get here by searching send back to home page
    if (!isset($_POST['search']))   {

        header("Location: index.php");


    }

//fix the query
//query the db using connection and sql query
$query = mysqli_query($link, "SELECT advert.advert_id, advert.advert_title, advert.price,  FROM advert WHERE advert.advert_title, advert.make, advert.model LIKE '%".$_POST['search']."%'");    

if (mysqli_num_rows($query)!=0) {

    //fetch the text fields
    $rs = mysqli_fetch_assoc($query);

}



$query2 = mysqli_query($link,  media.FILE_NAME);

if (mysqli_num_rows($query2)!=0)    {

    //fetch the image file name fields
    $rs_ = mysqli_fetch_assoc($query2);

}


?>

<!-- for the results section on the website -->

<h3> Search Results </h3>

<?php

    if (mysqli_num_rows($query)!=0) {
        do { ?>

        <!--place in div for advert title -->
        <div class= "title_of_advert">
        <?php
        $title= $rs['advert_title'];    
        $advert=$rs['$advert_id'];
        echo "<a href=\"advert.php?data=$advert\">
        $title</a>"; //display advert title with link to advert page

        ?> 
        </div>

        <!-- place in div for advert price  -->
        <div class= "price_of_advert">
        <?php echo $rs['price']; ?> 
        </div>

        <!-- place in display the thumbnail -->
        <div class= "thumbnail_of_ad" >
        <?php 

        $filepath = "http://localhost/projects/FYP/user_data/";
        $src= $filepath.$rs_['FILE_NAME'];

        echo "<img src=\".$src."\"/>"; 

        ?> 
        </div> 

<?php           } while($rs=mysqli_fetch_assoc($query));

    } else {

        echo "No Results have been found";

    }

?>

advert.php?= $ ID

    <?php


include (connect.php);

    $id = (isset($_GET['data'])) //? (int)$_GET['data'] : 1;
    // $id should be in the where clause of the mysqli_query.

    //fix the query
    //it should extract all data regarding the id of the advert
    $query = mysqli_query($link, "SELECT * FROM advert WHERE advert.advert_id='%".$id."%'");


    //for the text fields
    if (mysqli_num_rows($query)!=0) {

    //fetch the text fields results
    $rs = mysqli_fetch_assoc($query);

    }


<?

<?php

    if (mysqli_num_rows($query)!=0) {
        do { ?>

<!-- put this into the div class for advert title -->
<div class="">
 <?php
 $title= $rs['advert_title'];
 echo "<h1> $rs['advert_title'] </h1>";
 ?>
 </div>

 <!-- put this into the div class for text fields -->
 <div class="">
<?php
$dealer_name = $_rs['dealer_name']);
$phone_number = $_rs['phone_number']);
$make = $_rs['make']);
$model = $_rs['model']);
$type = $_rs['type']);
$year = $_rs['year']);
$price = $_rs['price']);
$condition_ = $_rs['condition_']);

echo "<h3> Vehicle Details </h3> <br> </br>
     Dealer Name : $dealer_name<br>
     Phone Number : $phone_number<br>
     Make : $make<br>
     Model : $model<br>
     Price :£ $price<br>
     Type : $type<br>
     Year : $year<br>
     Condition : $condition_";
?>
</div>

<?php           } while($rs=mysqli_fetch_assoc($query));

    } else {

        echo "No Results have been found";

    }

?>


<!-- place in images div -->
<div class= "">
<?php 

$a= array();
$query= mysqli_query($link, "SELECT FILE_NAME FROM media WHERE media.advert_id='%".$id."%'");
while($row = mysqli_fetch_assoc($query));
 $a[]= $row;

for ($i = 1; $i <= 10; $i++) {
 foreach ($a as $row)
   echo "<img src='http://localhost/projects/FYP/user_data/{$row['FILE_NAME']}'/>";
}

/***
 $filepath = "";
 $src= $filepath.$rs_['FILE_NAME'];

 echo "<img src=\".$src."\"/>"; 
**/

 ?>
</div>

1 个答案:

答案 0 :(得分:0)

在您的查询中,您在,之前有额外的fromwhere子句的条件也是错误的:

$query = mysqli_query($link, "SELECT advert.advert_id, advert.advert_title, advert.price,  FROM advert WHERE advert.advert_title, advert.make, advert.model LIKE '%".$_POST['search']."%'");

应该是:

$query = mysqli_query($link, "SELECT advert.advert_id, advert.advert_title, advert.price  FROM advert WHERE advert.advert_title LIKE '%".$_POST['search']."%'" OR advert.make LIKE '%".$_POST['search']."%'" OR advert.model LIKE '%".$_POST['search']."%'");   

同样在第二部分的where子句中,=使用了%like$query = mysqli_query($link, "SELECT * FROM advert WHERE advert.advert_id LIKE '%".$id."%'"); 如下:

{{1}}

您始终可以回显查询并在MySQL客户端中将其作为独立查询运行,并查看您的查询是否正常工作并返回任何结果。