简体   繁体   中英

Retrieve search results | PHP & SQL

I have a table created with all the fields necessary like (ID, Name, surname, etc.)

In search php file, when you type the ID it shows you all the information corresponding of this ID, eg (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.

QUESTION: How can I call to a custom search result from other php file?

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.

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,

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.

<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,

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

<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".

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.

 <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

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

$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!! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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