簡體   English   中英

如何使用php創建搜索結果詳細信息頁面

[英]how do i make a search results details page using php

我是一名葯劑師,致力於一個項目,用戶可以從我的數據庫中獲取葯品信息。 我已經進入搜索結果頁面,但是無法進入詳細信息頁面。 這是我的search.php頁面:

<?php
$db_hostname = 'localhost';
$db_username = 'root';
$db_password = '';
$db_database = 'drug';

// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($db_database, $con);
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
<div>

  <?php
if (!empty($_REQUEST['term'])) {

$term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM drugs WHERE GenericName LIKE '%".$term."%' OR     BrandName LIKE '%".$term."%' OR Pharmacologicalclass LIKE '%".$term."%' OR     ManufacturedBy LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

while ($row = mysql_fetch_array($r_query)){ 
echo '<a href="results.php?id='.$row['GenericName'].'">        <h4>'.$row['BrandName'].'</h4></a>'; 
echo '<br />Generic Name: ' .$row['GenericName'];  

echo '<br /> Dosage Form: '.$row['Dosage form'];  
echo '<br /> Pharmacological Class: '.$row['Pharmacologicalclass'];  
echo '<br /> Indications: '.$row['Indications']; 
echo '<br /> Manufactured By: '.$row['ManufacturedBy'];   
}  

}
?>
</div>
    </body>
</html>

在詳細信息頁面上,我想包括GenericName,BrandName,Dosageform,Strength,適應症,相互作用,副作用,懷孕類別,葯理學類別,作用方式和制造者(所有葯物表中的行)。

<html>
    <head><title>results - details</title></head>
    <body>
        <?php
            /* 
                "mysql_*" - the old mysql functions are deprecated and their use is discouraged
                You would be wise to take time now to implement mysqli in conjunction with
                prepared statements to avoid sql injection attacks.

                That said, below semi-pseudo code might help you get started

            */

            if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_GET['id'] ) ){

                $sql='select * from `drugs` where `id`='.$_GET['id'];
                $res=mysql_query( $sql, $con );

                if( $res ){
                    while( $rs=mysql_fetch_object( $res ) ){
                        echo $rs->GenericName, $rs->BrandName, $rs->Dosageform, $rs->Strength; /* etc */
                    }
                } else {
                    echo 'no results';
                }
                mysql_close( $con );
            }
        ?>
    </body>
</html>

我能夠做到。 如果有人遇到同樣的問題,這是我的代碼。 Search.php

// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($db_database, $con);
?>
 <form action="search.php" method="post">


<input type="text" class="form-control" name="term" placeholder="Search" /> 

<input type="submit" value="Submit" />  
</form> 
</div></div>
<div>

  <div class="container">
     <section class="col-xs-12 col-sm-6 col-md-12">

    <article class="search-result row">

  <?php
if (!empty($_REQUEST['term'])) {

$term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM drugs WHERE GenericName LIKE '%".$term."%' OR     BrandName LIKE '%".$term."%' OR Pharmacologicalclass LIKE '%".$term."%' OR     ManufacturedBy LIKE '%".$term."%' "; 
?>

<div style="">
      Results for: <font color="blue" style="font:bold 20px 'Aleo';"><?php     echo     $term;?></font>
      </div>
      <?php $r_query = mysql_query($sql); 

while ($row = mysql_fetch_array($r_query)){

echo '<a href="results.php?id='.$row['id'].'"><h4>'.$row['BrandName']. ":".     $row['GenericName']. " ".$row['Dosageform']. " ".$row['Strength'].'</h4></a>';     //links to results.php by id since id is unique    
echo ' '.$row['Pharmacologicalclass'];  
echo '<br /> uses: '.$row['Indications'];    
}  

}
?>

這是results.php

<?php
$connect = mysqli_connect("localhost","root","mypassword","drug");
?>
<?php
if(isset($_GET['id'])){ // checks if id was posted

$id = (int)$_GET['id'];

$query_fetch = mysqli_query($connect,"SELECT * FROM drugs WHERE ID = $id");    // id that was posted by search.php

 while($show = mysqli_fetch_array($query_fetch)){ 
  echo  " ".$show['BrandName']. ": &nbsp;" . $show['GenericName']. "      &nbsp;".$show['Strength']." <br></a>";

echo '<br /> <h3> <h3>Indications:</h3>'.$show['Indications'];  // these     rows are displayed to the user
echo '<br /> <h3> Pharmacological Class:</h3> '.$show['Precautions'];  
echo '<br /> <h3> Drug interactions:</h3> '.$show['Interactions']; 
echo '<br /> <h3> Undesirable effects:</h3> '.$show['SideEffects'];
echo '<br /> <h3> Use in pregnacy:</h3> '.$show['PREGNANCYcategory'];  
echo '<br /> <h3> Pharmacological Class:</h3>     '.$show['Pharmacologicalclass'];  
echo '<br /> <h3> Mode of action:</h3> '.$show['ModeOfAction']; 
echo '<br /> <h3> Manufactured By:</h3> '.$show['ManufacturedBy'];   
}  

}
?>   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM