ajax data response always 0 in php mysql

前端 未结 2 1291
清歌不尽
清歌不尽 2021-01-29 07:28

I want to load some data from mysql to a div via ajax php. The id field which is auto increment is always equal to zero.

I have tried using get, post etc but none is wor

2条回答
  •  猫巷女王i
    2021-01-29 07:57

    I assume that customer_id is a unique key in your database and only one row is returned for each id in your database. So, how to correctly use PDO statements:

    $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'];
    }
    

    EDIT Also, change your ajax request like this:

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

提交回复
热议问题