2015-08-19 276 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