2015-08-19 220 views
0

以下代碼似乎無法正常工作。我是PHP和jQuery的新手。PHP和MySQLi查詢總是返回0

PHP:

<?php 

//if (!defined('BOOTSTRAP')) { die('Access denied'); } 

//if we got something through $_POST 
if (isset($_POST['postcode_locator_search'])) { 
    // here you would normally include some database connection 
    //include('config.local.php'); 

    //Open a new connection to the MySQL server 
    $mysqli = new mysqli('localhost','test','[email protected])ukmd[0bm','test'); 

    //Output any connection error 
    if ($mysqli->connect_error) { 
     die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); 
    } 

    // never trust what user wrote! We must ALWAYS sanitize user input 
    $postcode_q = mysqli_real_escape_string($mysqli, $_POST['postcode_locator_search']); 
    $postcode_q = htmlentities($postcode_q); 

    // A select query. $result will be a `mysqli_result` object if successful 
    $result = mysqli_query("SELECT description FROM cscart_postcode_location_descriptions WHERE cscart_postcode_location_descriptions LIKE '%" . $postcode_q . "%' ORDER BY cscart_postcode_location_descriptions LIMIT 1"); 

    if($result === false) { 
     // Handle failure - log the error, notify administrator, etc. 
     echo '1'; 
    } else { 
     // Fetch all the rows in an array 
     echo '0'; 
    } 

    $mysqli->close(); 

} 
?> 

JS/HTML:

{assign var="prod_id" value=$product.product_id} 

<form action="search_postcode.php" method="post" class="postcode_locator_form" name="postcode_locator_form"> 
    <div class="ty-control-group"> 
     <label for="postcode_locator_search{$block.block_id}" class="ty-control-group__title">{__("postcode_search")}</label> 
     <p class="filling-notice">{__("postcode_search_desc")}</p> 
     <div class="ty-input-append ty-m-none"> 
      <input type="text" size="20" class="ty-input-text" id="postcode_locator_search" name="postcode_locator_search" value="{$postcode_locator_search.q}" /> 
      {include file="buttons/go.tpl" but_name="postcode_locator.search" alt=__("search")} 
     </div> 

    </div> 
</form> 

<div class="filling-status filling-success"> 
    <h3>Add filling to your bean bag</h3> 
    <p>Searched postcode: <span class="searched-postcode"></span></p> 
    <p class="beans-msg">{__("add_some_beans_success")} <a href="{"checkout.add_bean_bag_filling&product_id=`$product.product_id`"|fn_url}">{__("click_here")}</a></p> 
</div> 
<div class="filling-status filling-failure"> 
    <h3>Add filling to your bean bag</h3> 
    <p>Searched postcode: <span class="searched-postcode"></span></p> 
    <p class="beans-msg">{__("add_some_beans_error")}</p> 
</div> 

<script> 
$(function() { 

    $(".filling-status").hide(); 
    $(".postcode_locator_form .ty-btn-go").click(function() { 
     // getting the value that user typed 
     var searchString = $("#postcode_locator_search").val(); 
     // forming the queryString 
     var data   = 'postcode_locator_search='+ searchString; 

     // if searchString is not empty 
     if(searchString) { 
      // ajax call 
      $.ajax({ 
       type: "POST", 
       url: "search_postcode.php", 
       data: data, 
       beforeSend: function(html) { // this happens before actual call 
        $(".searched-postcode").html(searchString); 
       }, 
       success: function(data){ // this happens after we get results 
        console.log(data); 
        if(data == '0'){ 
         $(".filling-status.filling-success").show(); 
        } else if(data == '1'){ 
         $(".filling-status.filling-failure").show(); 
        } 
       } 
      });  
     } 
     return false; 
    }); 
}); 
</script> 

的通信是所有工作,但它總是返回0從什麼我搜索了成功,似乎不檢查數據庫結果。

我需要的是如果我搜索一些東西,它是一個匹配,返回0作爲成功,但如果找不到/匹配返回1作爲失敗。

+0

因爲它說你的病情。它讀取所有數據和回顯字符串0; – aldrin27

+0

我該如何解決它?對不起,即時通訊新的PHP,所以試驗和錯誤,直到現在和差不多2am哈哈 – James

+0

你運行查詢時記錄了你收到的錯誤?好像你的查詢中有無效的表/字段名稱。您的表格和您的字段名稱是否都是「cscart_postcode_location_descriptions」? – DGS

回答

0

使用mysqli_num_rows檢測,如果你有一個結果

if($result === false or mysqli_num_rows($result) === 0) { 
    echo '1'; 
} 

我會建議打破這兩大如果條件雖然讓你從查詢分開處理錯誤沒有結果

+0

我試過,但與其他答案相同,我鍵入的東西,這不是一個有效的結果和東西這應該是和兩個返回有效? – James

+0

什麼是無效的?當你期望它失敗並且看到它實際上正在查詢的內容時,回顯出你的查詢 – DGS

+0

以及它總是在控制檯上返回0 .... – James

1

如果你想檢索您的數據:

$result = mysqli_query("SELECT description FROMcscart_postcode_location_descriptions WHERE cscart_postcode_location_descriptions LIKE '%" . $postcode_q . "%' ORDER BY cscart_postcode_location_descriptions LIMIT 1"); 

if($result === false) { 
    // Handle failure - log the error, notify administrator, etc. 
    echo '1'; 
} else { 
    // Fetch all the rows in an array 
    while($row = mysqli_fetch_assoc($result)){ 
    echo $row['id']; //prints the resulted id 
    } 
} 
+0

請參閱@DGS答案。 – aldrin27

+0

我嘗試了兩種解決方案,但總是返回0,即使是隨機產生的結果也是如此:(也許我的查詢在某種方式是錯誤的嗎? – James

+0

可能是你的代碼中的問題'$ postcode_q = mysqli_real_escape_string($ mysqli ,$ _POST ['postcode_locator_search']); $ postcode_q = htmlentities($ postcode_q);' – aldrin27