问题描述
我想通过ajax php将一些数据从mysql加载到div.自动递增的id字段始终等于零.
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.
我尝试使用get,post等,但是没有任何作用
I have tried using get, post etc but none is working
<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)完成后应显示用户记录
The result is not echo the firs_name because the $id is always 0.I want to get the individual $id(auto_increment)when is done it should display user record
推荐答案
从调试数据库的实际结果开始.
Start with debugging your actual result from the database.
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变量中输出结果.我相信您会发现问题所在.
1) You are using <?php echo $row['first_name'];?>
. If you inspected the resultset you could see what is wrong with that. Just output the result in the (wrongly named) $row variable with print_r() and the like. I am sure you will see what went wrong.
2)我强烈建议再次使用$ _REQUEST.这是懒惰且容易出错.您知道'customer_id'的来源吗?会议?曲奇饼?邮政?还是得到?如果要通过GET =>传递信息,请使用GET
2) I strong advise AGAINST using $_REQUEST. It is lazy and errorprone. Do you know where the 'customer_id' came from? Session? Cookie? POST? Or Get? If you are passing information via GET => use GET
这篇关于ajax数据响应在php mysql中始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!