2017-05-31 198 views
0

我有一個Wordpress頁面,我正在使用從Amazon AWS上託管的數據庫中提取的搜索功能。在新頁面顯示搜索結果

在搜索時,我可以很好地提取數據,但結果顯示在搜索欄下方。
我希望結果顯示在其單獨的頁面上。

我已經嘗試了幾個建議,使用以前提出的問題,但沒有任何工作。

這裏是我的代碼:

<?php 
/* Template Name: Database */ 
?> 

<?php 
global $query_string; 

$query_args = explode("&", $query_string); 
$search_query = array(); 

if(strlen($query_string) > 0) { 
    foreach($query_args as $key => $string) { 
    $query_split = explode("=", $string); 
    $search_query[$query_split[0]] = urldecode($query_split[1]); 
    } // foreach 
} //if 

$search = new WP_Query($search_query); 
?> 


<?php get_header(); ?> 
<?php get_footer(); ?> 

<?php 
$db_hostname = 'xxxxxxxxxxxxxxx'; 
$db_username = 'xxxxxxxxxxxxxxx'; 
$db_password = 'xxxxxxxxxxxxxxx'; 
$db_database = 'xxxxxxxxxxxxxxx'; 

$con = mysql_connect($db_hostname,$db_username,$db_password); 
if (!$con){ 
    die('404 Could not connect to server: ' . mysql_error()); 
} 

mysql_select_db($db_database, $con); 

    global $sf_options; 
    $sidebar_config = $sf_options['archive_sidebar_config']; 
    $left_sidebar = $sf_options['archive_sidebar_left']; 
    $right_sidebar = $sf_options['archive_sidebar_right']; 
    $blog_type  = $sf_options['archive_display_type']; 

    if ($blog_type == "masonry" || $blog_type == "masonry-fw") { 
     global $sf_include_imagesLoaded; 
     $sf_include_imagesLoaded = true; 
    } 
?> 

<div class="container-fluid"> 
    <center> 
    <form role="" method="get" > 
     <p style="color: #fff;">Search products and services for your business. We make it easier<br />to manage your business operations and make informed purchasing decisions.</p> 
     <input type="text" name="term" placeholder="Start typing to find a product or business"/> 
     <span><input type="submit" value="" class="btn btn-default" /></span> 
    </form> 
    </center> 
</div> 

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

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

    $sql = "SELECT * FROM wpfr_listing WHERE Description LIKE '%".$term."%'"; 
    $r_query = mysql_query($sql); 

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

    echo '<br /> Company: ' .$row['title']; 
    echo '<br /> Certs: '.$row['certs']; 
    echo '<br /> Certifier: '.$row['certifier']; 
    echo '<br /> Address: '.$row['address']; 
    echo '<br /> Country: '.$row['country']; 
    echo '<br /> State: '.$row['state']; 
    echo '<br /> City: '.$row['city']; 
    } 

} 
?> 

提供任何幫助深表感謝。

+0

男孩哦這個代碼是如此的不堪一擊。爲了幫助你解決問題:在你的表單之前添加if(empty($ _REQUEST ['term']))',然後將if下面的表單改爲else。 – JapanGuy

+0

什麼使得這個代碼易受攻擊?它是由我們不再使用的開發人員編寫的。 – MARVNVRAM

+0

不建議使用mysql_命令(更改爲mysqli_),並將憑據連接到同一文件中的db不是一種好的做法。總的來說,比使用mysqli_更好地切換到PDO。 http://php.net/manual/en/ref.pdo-mysql.php – JapanGuy

回答

1

寫檢索算法結果頁上的此頁面

<?php if (!empty($_REQUEST['term'])) { 
header("location: page_url?term=".$_REQUEST['term']); 
} 
?> 

上,並

<?php 
$term = mysql_real_escape_string($_GET['term']); 
    $sql = "SELECT * FROM wpfr_listing WHERE Description LIKE '%".$term."%'"; 
    $r_query = mysql_query($sql); 
    while ($row = mysql_fetch_array($r_query)){ 
    echo '<br /> Company: ' .$row['title']; 
    echo '<br /> Certs: '.$row['certs']; 
    echo '<br /> Certifier: '.$row['certifier']; 
    echo '<br /> Address: '.$row['address']; 
    echo '<br /> Country: '.$row['country']; echo '<br /> State: '.$row['state']; 
    echo '<br /> City: '.$row['city']; 
    } 
    ?> 
+0

謝謝。這正是我所期待的! – MARVNVRAM

+0

然後請接受答案 –

0

既然你想通過PHP完成這個任務,那麼你就去吧。

1)創建一個新頁面。 2)讓它處理您在此處使用的查詢的GET數據。 3)打印您的搜索結果,如您在本頁面中所做的那樣。

現在,在這個頁面中,你需要做一個頭部重定向到該頁面頭部重定向的語法就像這樣。

header('Location: http://www.example.com/?sql = <Your Query above>'); 

這應該爲你做。希望這可以幫助。隨意評論,如果你需要任何進一步的信息。