我创建了一个表,其中包含所有必要的字段,例如(ID、姓名、姓氏等)
在搜索php文件中,当您输入ID时,它会显示该ID对应的所有信息,例如(ID=1,名称=Jack)
我的想法:当我进行自定义搜索时,我想在 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>
最佳答案
这就是我可以向您展示的内容。
你是第一个 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 的响应文本并将其添加到 ID 为“moreInfo”的 DIV
中。
关于php - 检索搜索结果 | PHP 和 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46642642/