2016-03-08 173 views
1

的WordPress 4.4.2主題=從WPZoom的WordPress:含有表格和自定義表的查詢自定義模板

我已經創建了一個包含狗的自定義表的SQL查詢自定義模板Meeta。查詢通過WPDB類正常工作,我可以以可接受的格式顯示輸出數據。

現在在相同的自定義模板中,我插入一個php表單以允許用戶輸入搜索條件。

表單顯示正常,但查詢立即執行而無需等待用戶點擊提交,因此我無法輸入任何搜索條件。我應該怎麼做才能保留查詢,直到表單被提交?

,如果我介紹主頁錯誤在其他地方顯示返回而不顯示錯誤,我已經擺弄周圍了一段時間,很多次。我是否必須打開錯誤顯示或將它發送到某處的日誌中。

 <?php the_content(); ?> 
       //================================= 
       <?php 

       function test_input($data) { 
       $data = trim($data); 
       $data = stripslashes($data); 
       $data = htmlspecialchars($data); 
       return $data; 
       } 

       // set variables to wildcard 
       $dog_name = "%"; 
       ?> 

       <h2>Chercher dans la base</h2> 
       <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

       Dog_name: <input type="text" dog_name="dog_name" value="<?php echo $dog_name;?>"> 

       <input type="submit" name="submit" value="Submit"> 

       </form> 

       <?php 
       $dog_name = test_input($_POST["dog_name"]); 

       global $wpdb; 

       $query = "SELECT offspring.dog_name, offspring.sex, offspring.color, offspring.dob, offspring.cotation, offspring.titles, offspring.hd, offspring.titles, offspring.eyes, offspring.dna, offspring.born_in, offspring.prod_prop, sire.dog_name AS sire, dam.dog_name AS lice 
        FROM BP_dog AS offspring 
        JOIN BP_dog AS sire ON sire.dog_id = offspring.sire_id 
        JOIN BP_dog AS dam ON dam.dog_id = offspring.dam_id 
        ORDER BY dog_name"; 
       word      
        $results = $wpdb->get_results($query); 

        $num_rows = $wpdb->num_rows; 
        print "Number of rows = $num_rows.<br>"; 

        foreach ($wpdb->get_results($query)as $row) { 
        print $row->dog_name . "\t"; 
        print $row->sex . "\t"; 
        print $row->dob . "\t"; 
        print $row->color . "\t"; 
        print $row->cotation . "\t"; 
        print $row->titles . "\t"; 
        print $row->hd . "\t"; 
        print $row->eyes . "\t"; 
        print $row->dna . "\t"; 
        print $row->born_in . "<br>"; 
        print "Sire = " . $row->sire . "\t"; 
        print "Lice = " . $row->lice . "<br>"; 
        print $row->prod_prop . "<br><br>"; 
        } 
        ?> 

回答

0

如果頁面加載/重載,您寫的查詢將直接運行。使用isset執行代碼只ID您$_POST數據。

<h2>Chercher dans la base</h2> 
<form method="post" action="http://localhost/wordpress/?page_id=4"> 
Dog_name: <input type="text" name="dog_name" value="<?php echo $dog_name;?>"> 
<input type="submit" name="submit" value="Submit"> 
</form> 
<?php 
if (isset($_POST["dog_name"])) { 
function test_input($data) { 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
return $data; 
} 
$dog_name = test_input($_POST["dog_name"]); 

global $wpdb; 

$query = "SELECT offspring.dog_name, offspring.sex, offspring.color, offspring.dob, offspring.cotation, offspring.titles, offspring.hd, offspring.titles, offspring.eyes, offspring.dna, offspring.born_in, offspring.prod_prop, sire.dog_name AS sire, dam.dog_name AS lice 
    FROM BP_dog AS offspring 
    JOIN BP_dog AS sire ON sire.dog_id = offspring.sire_id 
    JOIN BP_dog AS dam ON dam.dog_id = offspring.dam_id 
    ORDER BY dog_name"; 

    $results = $wpdb->get_results($query); 

    $num_rows = $wpdb->num_rows; 
    foreach ($wpdb->get_results($query)as $row) { 
    print $row->dog_name . "\t"; 
    print $row->sex . "\t"; 
    print $row->dob . "\t"; 
    print $row->color . "\t"; 
    print $row->cotation . "\t"; 
    print $row->titles . "\t"; 
    print $row->hd . "\t"; 
    print $row->eyes . "\t"; 
    print $row->dna . "\t"; 
    print $row->born_in . "<br>"; 
    print "Sire = " . $row->sire . "\t"; 
    print "Lice = " . $row->lice . "<br>"; 
    print $row->prod_prop . "<br><br>"; 
    } 
} 
?> 

我還修改了幾件事情上的代碼:

  1. 刪除 「word」 在第46行
  2. 變化input type="text" dog_name="dog_name"input type="text" name="dog_name"

  3. 刪除$dog_name = "%";。不知道爲什麼你正在使用這個

+0

非常感謝溼婆。我得到了正確運行和查詢執行感謝您的建議的形式。 – Gary

+0

謝謝加里:) –