ajax数据响应在php mysql中始终为0

时间:2019-08-05 09:32:06

标签: php mysql ajax

我想通过ajax php将一些数据从mysql加载到div。自动递增的id字段始终等于零。

我尝试使用get,post等,但是没有任何作用

<script>
  $(document).ready(function(){

   $(document).on('click', '#getUser', function(e){

    e.preventDefault();

    var uid = $(this).data("customer_id");   // it will get id of clicked row

    $('#txtHint').html(''); // leave it blank before ajax call
    $('#mySidenav').show();      // load ajax loader

    $.ajax({
      url: 'getCustomerDetails.php',
      type: 'GET',
      data: 'customer_id='+uid,
      dataType: 'html'
    })
    .done(function(data){
      console.log(data);  
      $('#txtHint').html('');    
      $('#txtHint').html(data); // load response 
      $('#modal-loader').hide();      // hide ajax loader 
    })
    .fail(function(){
      $('#txtHint').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
      $('#modal-loader').hide();
    });

  });

 });

</script>


        <?php

include_once('../../config/dbconfig.php');


    if (isset($_REQUEST['customer_id'])) {

        $id = intval($_REQUEST['customer_id']);
        $query = "SELECT * FROM customers WHERE customer_id=:id";
        $stmt = $pdo->prepare( $query );
        $stmt->execute(array(':id'=>$id));
        $row=$stmt->setFetchMode(PDO::FETCH_ASSOC);


        ?>
        <div class="row">
            <div class="col-md-1">
            <div class="col-md-4" >
                <?php echo $row['first_name'];?>
            </div>
            <div class="col-md-6">

            </div>
        </div>
    </div>


    <?php echo $id;?><br/>


<?php
}

?>

由于$ id始终为0,因此结果没有回显firs_name。 我想获取单个$ id(auto_increment) 完成后应显示用户记录

2 个答案:

答案 0 :(得分:1)

从调试数据库的实际结果开始。

if (isset($_REQUEST['customer_id'])) {

        $id = intval($_REQUEST['customer_id']);
        $query = "SELECT * FROM customers WHERE customer_id=:id";
        $stmt = $pdo->prepare( $query );
        $stmt->execute(array(':id'=>$id));
        $row=$stmt->setFetchMode(PDO::FETCH_ASSOC);

您不检查错误。

两个建议:

1)您正在使用<?php echo $row['first_name'];?>。如果检查了结果集,您会发现问题出在哪里。只需使用print_r()等在(错误命名的)$ row变量中输出结果。我相信您会发现问题所在。

2)我强烈建议再次使用$ _REQUEST。这是懒惰且容易出错。您知道'customer_id'的来源吗?会议?曲奇饼?发布?还是得到?如果要通过GET =>传递信息,请使用GET

答案 1 :(得分:0)

我假设customer_id是数据库中的唯一键,并且数据库中的每个ID仅返回一行。因此,如何正确使用PDO语句:

$sql = "SELECT * FROM customers WHERE customer_id=:id";
//Prepare your SELECT statement.
$statement = $pdo->prepare($sql);
//The Primary Key of the row that we want to select.
$id = intval($_REQUEST['customer_id']);
//I highly recomment not to use $_REQUEST, use $_GET or even better $_POST

//Bind our value to the paramater :id.
$statement->bindValue(':id', $id);

//Execute our SELECT statement.
$statement->execute();

//Fetch the row.
$row = $statement->fetch(PDO::FETCH_ASSOC);

//If $row is FALSE, then no row was returned.
if($row === false){
    echo $id . ' not found!';
} else{
    echo 'Found: ' . $row['first_name'];
}

编辑 另外,这样更改ajax请求:

$.ajax({
  url: 'getCustomerDetails.php',
  type: 'POST',
  data: {customer_id:uid}
})