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

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

最佳答案

在您的查询中,from 之前有一个额外的 ,,而且 where 子句的条件也是错误的:

$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."%'");

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

关于php - 使用 PHP 将搜索结果链接到 MySQL 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239448/

相关文章:

search - 使用Elasticsearch搜索多态数据

php - 如何使用codeigniter php找到搜索功能最匹配的关键字

java - 如何在lucene文档中存储数值?

php - 如何创建一个 laravel 哈希密码

mysql - 为什么 Yii2 ActiveRecord joinWith 进行 2 个选择

mysql - 在不知道 Informix 中的外键名称的情况下删除两个表的列之间的外键约束?

c# - 存储需要重复使用的密码的最佳实践?

javascript - 如何将 Facebook PHP 访问 token 与用于 FB.UI 对话框的 FB Javascript SDK 一起使用

javascript - 根据 ajax 的值终止或停止

php - 条件 preg_replace 用于可选的反向引用替换