2017-10-09 98 views
0

我有一個表格,其中包含所有必要的字段(ID,姓名等)檢索搜索結果| PHP&SQL

在搜索php文件中,當您鍵入ID時,它會顯示與此ID相對應的所有信息,例如(ID = 1,name = 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> 
+0

'問題:我如何從其他php文件調用自定義搜索結果?'好吧,您通過'_ _GET'發送了一些不被建議的東西。但是你會'header(「Location:page.php?idNumber = search term」)' – IsThisJavascript

+0

@ WillParky93我應該使用post方法嗎?或者你認爲我是最好的選擇。 非常感謝:) – Dann

+0

是的使用後。如果你使用post,你將無法足夠的標題,但你總是可以使用'ajax'來查詢你的php頁面。 – IsThisJavascript

回答

0

那麼我終於用另一種方式做到了。

result.php只添加了一個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> 

這裏來的代碼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>'; 
} 

希望它有助於進一步解決問題。非常感謝所有的幫助! :)

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的響應文本,並將其添加到標識爲「moreInfo」的DIV

+0

首先,感謝您的幫助。我試圖做但它顯示我回 未定義的索引(idNumber):$ IDNUMBER = $ _ GET ['idNumber']; 未定義變量(conn):$ stmt = $ conn-> prepare(「select * from madea where idNumber =?」); – Dann