我有一个从mysql数据库中提取信息的函数。该函数已经过测试,当从常规的html表单提交按钮调用它时可以工作。然而,我一直在尝试实现ajax,我调用了完全相同的函数,但它不起作用,我不知道为什么。
这是数据库函数。我删除了不必要的代码,使其可读性更强。
function itemUPCsearch($upc)
{
$conn = localConnection();
$query = "SELECT Items.UPC, availability, description, price, brand, perqty, item_type, unit_type, size, discount, serv_per_cal, calories, calories_from_fat, total_fat, saturated_fat, trans_fat, cholesterol, sodium, total_carbohydrate, dietary_fiber, sugars, protein, vitamins, ingredients, full_image_path FROM Items LEFT JOIN Nutrition ON Items.UPC = Nutrition.UPC LEFT JOIN item_pic P ON P.UPC = Items.UPC WHERE Items.UPC = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "s", $upc);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $upc, $avail, $desc, $price, $brand, $perqty, $itype, $utype, $size, $discount, $servpercal, $cal, $calfromfat, $totfat, $satfat, $tranfat, $chol, $sod, $totcarb, $dietfib, $sugar, $prot, $vit, $ing, $image);
while(mysqli_stmt_fetch($stmt))
{
// Execution never makes it inside this loop
// Do stuff here (Code has been removed as it is not relevant
// To this question)
}
mysqli_stmt_close($stmt);
closeConnection($conn);
return $searchItems;
}
据我所知,一切似乎都很好。我已经在$conn和$stmt上进行了打印,我得到了我所期望的结果。我将这些与工作函数上那些对象的print_r进行了比较,得到了相同的结果。我已经打印了execute函数的结果,它正在返回true。与使用常规表单submit时相比,该查询从未更改。传入的值$upc是正确的,并且数据库中有相应的条目。事实上,当我将带有upc值的查询复制并粘贴到mysql中时,我会得到正确的结果。然而,出于某种原因,该函数从未进入while循环,即使它在传递相同的upc时使用form post调用相同的函数时也会进入while。
这是我的ajax脚本:
function loadCart(itemID,cart) {
var httpRequest;
makeRequest(itemID,cart);
function makeRequest(itemID,cart) {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = fillTheCart;
httpRequest.open('GET', "../controller/addToCartAjaxScript.php?upc="+itemID+"&cart="+cart,true);
httpRequest.send();
}
function fillTheCart() {
try {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
document.getElementById("cartView").innerHTML=httpRequest.responseText;
}
else {
alert('There was a problem with the request.');
}
}
}
catch( e ) {
alert('Caught Exception: ' + e.description);
}
}
}
这是ajax调用的脚本:
<?php
//include_once '../databaseFunctions/session.php';
include_once '../databaseFunctions/databaseConnection.php';
include_once '../databaseFunctions/databaseFunctions.php';
include_once '../controller/TableControllerHeader.php';
include_once '../controller/ShoppingCartControllerHeader.php';
include_once '../controller/UserControllerHeader.php';
include_once '../controller/functions.php';
$upc = intval($_GET['upc']);
$cartName = $_GET['cart'];
$cart = new Cart();
$customer = new Customer();
print $upc;
if($upc != ""){
$cart = addToCart($upc,$cartName);
}
else{
if(isset($_SESSION['customer'])){
$customer = unserialize($_SESSION['customer']);
$cart = $customer->FindCart($cartName);
if($cart = null){
$cart = new Cart();
}
}
}
print $cart->PrintCart();
?>
我知道ajax正在工作,因为print$cart->PrintCart();在ajax调用的脚本的末尾,它正在打印它应该打印的内容,并且它被放置在id为"cartView"的元素中,它只是不包括任何应该从数据库函数中提取的数据。
localConnection();itemUPCsearch函数中调用的函数位于databaseConnection.php脚本中,该脚本包含在ajax调用的脚本的开头。我在我的主脚本中也调用了这个脚本,但我认为这不是问题。我没有得到任何冲突错误,如果我去掉include语句,它会给出一个找不到localConnection()函数的错误。
考虑到函数中的所有内容看起来都是正确的,我不明白出了什么问题。如有任何帮助,我们将不胜感激!
编辑
我在数据库函数中的每个函数调用之后都打印了mysql_error(),并且在其中任何一个调用之后都没有得到错误消息。
好的,我终于解决了这个问题。在ajax调用的脚本中,我调用了intval($_GET['upc'])。intval函数是个问题。将其更改为$upc=$_GET['upc']解决了问题。