类的逻辑问题

时间:2013-11-24 21:24:33

标签: php jquery logic

首先,我将向您展示我的脚本,然后讨论我的问题:

的index.php:

$('.ui-selectable').on('update', function() {
    data = [];
    $(':not([data-id=""]).selected', this).each(function() {
        data.push( $(this).data('id') );
    });

    var input = JSON.stringify(data);
    var lines = input.split('.');
    var vari1 = lines[0];
    var vari2 = lines[1];

        $.ajax({
            url: "checkAvailability.php",
            type: 'POST',
            dataType: "json",
            data: 'username=' + JSON.stringify(data),
            success: function(data) {
                if (data.result == 1) {
                    $("#select-err").text(data.error);
                }
                else {
                    $("#select-err").text(data.error);
                }
            }
        });
});

select:
<span class="error" id="select-err"></span><br><br>

<ul class="ui-selectable" id="box">
<li data-id=100.100></li>
<li data-id=101.100></li>
<li data-id=102.100></li>
<li data-id=103.100></li>
<li data-id=104.100></li>
<li data-id=105.100></li>
<li data-id=106.100></li>
<li data-id=107.100></li>
<li data-id=108.100></li>
<li data-id=109.100></li>
<li data-id=110.100></li>
</ul>

checkAvailability.php:

include "storescripts/connect_to_mysql.php";

require 'AvailabilityChecker.php';

$config = array('DB' => $mysqli,
                'Table' => 'dumps',
                'Row' => 'dump_value',
                'Output' => true,
                'Format' => 'JSON');

$availabilityChecker = new AvailabilityChecker($config);

if($availabilityChecker->check_availability($_POST['username'])) {
    echo json_encode(array("error" => "not in DB", "result"=>1));
} else {
    echo json_encode(array("error" => "already in DB", "result"=>0));
} 

我的课程AvailabilityChecker.php:

class AvailabilityChecker {
    private $db;
    private $table;
    private $row;
    private $output;
    private $format; 
    public function __construct($config) {
        if(!is_array($config)) trigger_error('NO CONFIG');
        $isset = isset($config['DB'], $config['Table'], $config['Row']);
        if(!$isset) trigger_error('CONFIG NOT VALID');

        $this->format = (isset($config['Format']) && ($config['Format'] === 'JSON')) ? 'json' : 'default';
        $this->output = (isset($config['Output']) && ($config['Output'] === true)) ? true : false;

        $this->db = $config['DB'];
        $this->table = $config['Table'];
        $this->row = $config['Row'];
    }   
    public function check_availability($dataID) {

        $query = "SELECT dump_value FROM dumps WHERE dump_value = '".$dataID."'";
        global $mysqli;
        if ($stmt = $mysqli->prepare($query)) {

            $stmt->execute();
            $stmt->store_result();

            if($stmt->num_rows == 0) {
                return TRUE; //not in DB
            } elseif($stmt->num_rows >= 1) {
                return FALSE;  //already in DB
            }

            $stmt->close();
        }
    }  
}

现在,当我选择“data-id”已存在于我的数据库中的元素时,我得到:

“不在DB”

任何人都可以告诉我为什么?

更新:

$.ajax({
    url: "checkAvailability.php",
    type: 'POST',
    dataType: "json",
    data: {username:data},
    success: function(data) {
        if (data.result == 1) {
            $("#select-err").text(data.error);
        }
        else {
            $("#select-err").text(data.error);
        }
    }
});

0 个答案:

没有答案