通过 PHP-MySQL 搜索显示特定记录的结果 [英] Displaying result for a particular record through PHP- MySQL Search

查看:58
本文介绍了通过 PHP-MySQL 搜索显示特定记录的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用搜索条件进行数据库搜索后,我确实得到了所需的记录,但我不想显示每条记录的所有字段(85 个字段);相反,我只显示了其中的 5 个,然后希望提供一个单击以获取更多详细信息"的超链接,以便用户可以继续查看特定记录的所有字段.

After using the search criteria for my database search, I do get the desired records, but I dont want to display all the fields(85 fields) for each of the records; instead I've displayed only 5 of them, and then wish to give a hyperlink saying "Click for further Details", so that the users can then go ahead and view all the fields for a particular record.

例如,如果用户在搜索框中输入abc",并且数据库包含 5 个带有abc"的记录,那么它将显示所有 5 个记录,但只显示 5 个特定字段.但是要查看所有字段,例如第 1 个abc"记录,用户将单击更多详细信息",这会将他定向到包含该特定第一个 abc 记录的所有 85 个字段的另一个页面.

For eg, if the user enters "abc" in Search box, and the database contains 5 records with "abc", then it'll show all 5 records, but only 5 specific fields. But to view all the fields, for say the 1st "abc" record, the user will click on "further details", which will direct him to another page with all the 85 fields for that particular 1st abc record.

请指导我完成!我不知道要提供什么条件,因为我尝试使用 header(Location) 来执行此操作,但显示所有记录,甚至不采用特定处理的 sql 查询!

Kindly guide me through! I dont know what conditions to give, because i tried doing it with header(Location), but displays all the records, and doesn't even take that particular processed sql query!

推荐答案

以下是您可以执行的操作的示例.这是一个自引用搜索页面,首先使用通用查询来获取所有记录.然后,当它遍历每条记录时(仅抓取特定字段,它会创建一个表单,该表单使用带有主键/唯一 ID 的隐藏变量作为 POST 变量发送给自身(search.php).然后,当该 POST 变量被检测到,查询只抓取那 1 条特定记录,并在 WHILE 循环中使用 IF/THEN 语句,显示所有 80 多个字段.

Here's an example of what you could do. It's a self-referencing search page that first uses a generic query to get all records. Then, as it cycles through each record (only grabbing specific fields, it makes a form that uses a hidden variable with the Primary Key/Unique ID to send to itself (search.php) as a POST variable. Then, when that POST variable is detected, the query only grabs that 1 specific record and using an IF/THEN statement in the WHILE loop, shows all 80+ fields.

有更好的方法来查询和处理表单数据,但这应该能让您入门.如果有效,请选择此作为答案.

There are better ways to do querying and handling form data, but this should get you started. Please select this as an answer if it works.

<?php

    $query = "SELECT * FROM table"; 

    if (!empty($_POST['id'])) {
        $query = "SELECT * FROM table WHERE id_field = '" . $_POST['id'] . "'";
    }

    $query_info = mysql_query($query) or die(mysql_error()); 
    $query_info_count = mysql_num_rows($query_info);

    while($query_info_details = mysql_fetch_array($query_info)){

    if ($query_info_count > 1) {

        $field_a = $query_info_details['field_a'];
        $field_d = $query_info_details['field_d']; // Say this is the Primary Key
        $field_f = $query_info_details['field_f'];
        $field_g = $query_info_details['field_g'];
        $field_s = $query_info_details['field_s'];

        echo "<form method='post' action='search.php'>";

        echo "Name: " . $field_a . " - " . $field_f . " - ID: " . $field_d;
        echo " - Location: " . $field_g . "(" . $field_s . ")";

        echo "<input type='hidden' name='id' value='" . $field_d . "'>
        <input type='submit' value='View This Record'><br><br>";

    }
    else {

        // Display all fields for record using $query_info_details['field_X'] from above

    }

    }

?>

这篇关于通过 PHP-MySQL 搜索显示特定记录的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆