mysql查询无法在json代码中工作

时间:2014-07-26 07:13:25

标签: php ajax json

我已经为折扣优惠券返回了json Ajax代码...代码工作正常,但我会尽快在ajax页面中运行查询..响应停止工作

JavaScript代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
 $(document).ready(function() {


    $("#submit_btn").click(function() { 
        //get input field values
        var user_name = $('input[name=discountBox]').val(); 

        //simple validation at client's end
        //we simply change border color to red if empty field using .css()
        var proceed = true;
        if(user_name==""){ 
            $('input[name=discountBox]').css('border-color','red'); 
            proceed = false;
        }

        //everything looks good! proceed...
        if(proceed) {
        //data to be sent to server
            post_data = {'discountBox':user_name};

           //Ajax post data to server
            $.post('checking-discount.php', post_data, function(response){  

                //load json data from server and output message     
                if(response.type == 'error')
                {
                    output = '<div class="error">'+response.text+'</div>';
                }else{
                    output = '<div class="success_2">'+response.text+'</div>';

                    //reset values in all input fields
                    $('#contact_form input').val(''); 
                }

                $("#result").hide().html(output).slideDown();
            }, 'json');
       }
    });


    //reset previously set border colors and hide all message on .keyup()
    $("#contact_form input, #contact_form textarea").keyup(function() { 
        $("#contact_form input, #contact_form textarea").css('border-color',''); 
        $("#result").slideUp();
    });
});

ajax页面: 的检查-discount.php

if($_POST){
    include_once("db.php");

    //check if its an ajax request, exit if not
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

        //exit script outputting json data
        $output = json_encode(
        array(
            'type'=>'error', 
            'text' => 'Request must come from Ajax'
        ));

        die($output);
    } 

    //check $_POST vars are set, exit if any missing
    if(!isset($_POST["discountBox"]))
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Please enter the code!!'));
        die($output);
    }

    //Sanitize input data using PHP filter_var().
    $user_Name = filter_var($_POST["discountBox"], FILTER_SANITIZE_STRING);

    //additional php validation
    if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Discount Code is too short or empty!'));
        die($output);
    }

    $coupQuery = "select * tableName where field = '$user_Name'";
    $result = mysql_query($coupQuery) or die('MySql Error' . mysql_error());
    $row = mysql_fetch_array($result);

    //print_r($couRow);

    $discountAmt = 10;
    //proceed with PHP email.

    //if(count($couRow)>0){ 
        $output = json_encode(array('type'=>'message', 'text' => $coupQuery));
        die($output);
    //}else{
        //$output = json_encode(array('type'=>'error', 'text' => 'Could not find the Discount Code! Please check your code.'));
        //die($output);
    //}
}

每当我删除$ result和$ row代码时,它的查询工作正常,但不允许获取行..

请让我知道我错在哪里.. ???

2 个答案:

答案 0 :(得分:1)

编辑此行

$coupQuery = "select * tableName where field = '$user_Name'";

到(在from之前插入tableName

$coupQuery = "select * from tableName where field = '$user_Name'";

答案 1 :(得分:0)

我已经通过解决PHP中的一些编码纠正了这个问题...下面是代码中的修正

if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    //exit script outputting json data
    $output = json_encode(
    array(
        'type'=>'error', 
        'text' => 'Request must come from Ajax'
    ));
    die($output);
} 

//check $_POST vars are set, exit if any missing
if(!isset($_POST["discountBox"]))
{
    $output = json_encode(array('type'=>'error', 'text' => 'Please enter the code!!'));
    die($output);
}

//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["discountBox"], FILTER_SANITIZE_STRING);

//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
    $output = json_encode(array('type'=>'error', 'text' => 'Discount Code is too short or empty!'));
    die($output);
}
$total = $_GET["totalAmt"];
$date = date('Y-m-d');
$coupQuery = "select * from ghasitaram_coupon where  gha_coupon_code = '$user_Name' AND gha_coupon_status='1' AND '$date' between gha_coupon_startDate and gha_coupon_endDate AND $total between gha_coupon_startRange and gha_coupon_endRange";
$result = mysql_query($coupQuery) or die('MySql Error' . mysql_error());
$row = mysql_fetch_array($result);
$num_rows = mysql_num_rows($result);
$disPercent = $obj->stringEncryption(str_replace("%","",$row['gha_coupon_discount']));
//proceed with PHP email.

if($num_rows>0){    
    $output = json_encode(array('type'=>'message', 'text' => 'Applying code for Discount', 'code' => $disPercent));
    die($output);
}else{
    $output = json_encode(array('type'=>'error', 'text' => 'Could not find the Discount Code! Please check your code.'));
    die($output);
}