检索搜索结果| PHP& SQL

时间:2017-10-09 09:07:22

标签: php mysql sql codeigniter variables

我创建了一个包含所有必填字段的表,如(ID,Name,surname等)

在搜索php文件中,当您键入ID时,它会显示与此ID相对应的所有信息,例如(ID = 1,姓名=杰克)

我的想法:当我进行自定义搜索时,在php中我想添加一个链接到另一个显示相同搜索结果但附加信息的php文件。

问题:如何从其他php文件调用自定义搜索结果?

关于这个例子,如果我搜索ID = 2,我想链接到另一个显示该自定义搜索的更多信息的php文件“Extrainfo.php”。

以下是我使用的代码:

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

2 个答案:

答案 0 :(得分:0)

这是我可以告诉你的。

你是第一个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']; 

}

现在我使用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>

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();
}

现在在你的第一个php文件中可以有一个DIV,它将显示来自moreInfo.php的响应

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

AJAX脚本将以post方式发送数据,然后从第二个PHP中捕获响应文本,并将其添加到DIV ided作为&#34; moreInfo&#34;。

答案 1 :(得分:0)

好吧,我终于以另一种方式做到了。

result.php只添加了一个重定向到moreinfo.php的href,但是带有id号的查询字符串。

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

以下是moreinfo.php

中代码的另一部分

首先,获取先前由链接重定向的查询字符串上的id号,并将其放入变量中,以便在sql查询中使用它

$reportNumber = $_GET['idNumber'];

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

其余的,只显示我真正需要的结果:

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

希望它有助于进一步解决问题。非常感谢所有的帮助!! :)