简体   繁体   English

检索搜索结果| PHP和SQL

[英]Retrieve search results | PHP & SQL

I have a table created with all the fields necessary like (ID, Name, surname, etc.) 我创建了一个表格,其中包含所有必填字段,例如(ID,名称,姓氏等)

In search php file, when you type the ID it shows you all the information corresponding of this ID, eg (ID=1, name=Jack) 在搜索PHP文件中,当您键入ID时,它将显示与该ID相对应的所有信息,例如(ID = 1,name = Jack)

MY IDEA: When i do a custom search, inside the php I want to add a link to another php file that shows the same search result but with additional info. 我的想法:当我进行自定义搜索时,我想在php内添加指向另一个php文件的链接,该链接显示相同的搜索结果,但带有其他信息。

QUESTION: How can I call to a custom search result from other php file? 问题:如何从其他php文件调用自定义搜索结果?

Regarding this example, If I search for ID=2, I want to link to another php file "Extrainfo.php" that shows more info of that custom search. 关于此示例,如果我搜索ID = 2,我想链接到另一个php文件“ Extrainfo.php”,该文件显示了该自定义搜索的更多信息。

Here is the code I used: 这是我使用的代码:

//database connection

global $conn;
$servername = "localhost";  //host name
$username = "root"; //username
$password = ""; //password
$mysql_database = "info"; //database name

//mysqli prepared statement 

$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");

if(isset($_GET['idNumber']))
{
    $IDNUMBER =$_GET['idNumber'];
    $stmt = $conn->prepare("select * from madea where idNumber=? ");
    $stmt->bind_param('s',$IDNUMBER);
    $stmt->execute();
    $val =  $stmt->get_result();
    $row_count= $val->num_rows;

    if($row_count>0)
    {
        $result =$val->fetch_assoc(); 
        echo $result['idNumber']; 
        echo $result['name']; 
    }
    else
    {
        echo "identification_number not Match";
    }

    $stmt->close();
    $conn->close();

    // Probably need to save the variable to call in the other php file?

    $idNumber = $result['idNumber']; 

}

?>

<a href="extrainfo.php">Extrainfo</a>

This is what I can show you. 这就是我可以告诉你的。

You 1st php, 您第一个PHP,

if(isset($_GET['idNumber']))
{
    $IDNUMBER =$_GET['idNumber'];
    $stmt = $conn->prepare("select * from madea where idNumber=? ");
    $stmt->bind_param('s',$IDNUMBER);
    $stmt->execute();
    $val =  $stmt->get_result();
    $row_count= $val->num_rows;

    if($row_count>0)
    {
        $result =$val->fetch_assoc(); 
        echo $result['idNumber']; 
        echo $result['name'];
        echo "<a href="#" id=".$result['idNumber']." class="moreInfo">More Info</a>";
    }
    else
    {
        echo "identification_number not Match";
    }

    $stmt->close();
    $conn->close();

    // Probably need to save the variable to call in the other php file?

    $idNumber = $result['idNumber']; 

}

Now I'm using AJAX and Jquery so please link the appropriate libraries. 现在,我正在使用AJAX和Jquery,因此请链接适当的库。

<script type="text/javascript">
    $(document).ready(function(){
        $(document).on('click', '.moreInfo', function(){
            $.ajax({
            url: 'moreInfo.php',
            type: 'post',
            data: {
                'idNumber': $('.moreInfo').prop('id')
            }
            }).then(function (response) {
            $('#morInfoDiv').html(response);
            });
        })
    })
</script>

The moreInfo.php, moreInfo.php,

if(isset($_POST['idNumber']))
{
    $IDNUMBER =$_GET['idNumber'];
    $stmt = $conn->prepare("select * from madea where idNumber=? ");
    $stmt->bind_param('s',$IDNUMBER);
    $stmt->execute();
    $val =  $stmt->get_result();
    $row_count= $val->num_rows;

    if($row_count>0)
    {?>
        Name:<? echo $result['Name']; ?><br>        
        Address:<? echo $result['address']; ?><br>
        Date of Birth:<? echo $result['dob']; ?><br>
    <?php }
    else
    {
        echo "identification_number not Match";
    }

    $stmt->close();
    $conn->close();
}

Now in your 1st php file can have a DIV which will show the response from the moreInfo.php 现在在您的第一个php文件中可以有一个DIV ,该DIV将显示来自moreInfo.php的响应

<html>
<body>
    <div id="morInfoDiv"></div>
</body>
</html>

AJAX script will send the data in post method then capture the response text from the 2nd PHP and add it to the DIV ided as "moreInfo". AJAX脚本将以post方法发送数据,然后捕获第二个PHP的响应文本,并将其添加到ID为“ moreInfo”的DIV

Well I finally do it by another way. 好吧,我终于用另一种方式做到了。

result.php only added an href that redirects to a moreinfo.php but with the query string of the id number. result.php仅添加了一个href,该href重定向到moreinfo.php,但带有ID号的查询字符串。

 <a href="moreinfo.php?idNumber=<?php echo $idNumber?>" class="moreInfo" id="<?php echo $result['idNumber']; ?>" target="_blank">Download PDF INFO</a>

And here comes the other part of code in moreinfo.php 这是moreinfo.php中代码的另一部分

At first, get the id number on query string that it previously redirected by the link and get it into a variable to use it after in the sql query 首先,获取先前由链接重定向的查询字符串上的ID号,并将其放入变量以在sql查询中使用

$reportNumber = $_GET['idNumber'];

$result = mysqli_query($con,"SELECT * FROM madea where reportNumber='".$idNumber."'");

And the rest, only show the results what I really need: 其余的仅显示结果,我真正需要的是:

while($row = mysqli_fetch_array($result))
{
$html .= '<td>'.$row['idNumber'].'</td><td>' . $row['Name']. '</td>';
 }

Hope it helps to further issues. 希望它有助于进一步解决问题。 So appreciated for all the help!! 非常感谢所有帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM