0

这个 javascript/AJAX 在我的本地主机服务器上运行,但是当我将它移动到共享主机时,它现在会在 MySQL 调用中的非对象上抛出一个调用成员函数 execute() 的错误。

的HTML:

onclick="showTrending('page_views DESC', 'product.active= "y" 
AND product.deleted= "n" ', '12', 'popular')"

然后是javascript:

function showTrending(mysql_order, mysql_limit, limit, trend)
{
$.ajax({ type: "POST", 
url: '/ajax/product_trending.php', 
data: {Mysql_Order: mysql_order, Mysql_Limit: mysql_limit, Limit: limit},
cache: false, 
success: function(result) {
 // if productSubType array is defined and has at least one element, display subcategory list
if(result != 0){...

以及在出错的 AJAX 中调用的 PHP 文件:

//Retrieve subcategories for supplied product type
if(isset($_POST['Mysql_Order']) && isset($_POST['Mysql_Limit']) 
&& isset($_POST['Limit'])){
$mysql_order = $_POST['Mysql_Order'];
$mysql_limit = $_POST['Mysql_Limit'];
$limit = $_POST['Limit'];
//Overlay for wishlist
if(!isset($_SESSION['email'])){
    $item_wishlist = NULL;
} else{
    $item_wishlist = $_SESSION['id'];
}
//Get product records from db
require_once($GLOBALS['domain'].'includes/connection.inc.php');
$db = dbConnect();
$stmt = $db->stmt_init();


$stmt = $db->prepare("SELECT product.id, product.image_thumb, product.title,
    product.eng_title, product.price, seller.shop_name, seller.id FROM product 
    INNER JOIN seller ON product.seller_id=seller.id WHERE $mysql_limit ORDER BY
    $mysql_order LIMIT 0,$limit"); <-- This is the part that errors


$stmt->execute();
$stmt->bind_result($row['product_id'], $row['image_thumb'], $row['title'],
    $row['eng_title'], $row['price'], $row['shop_name'], $row['seller_id']);
$counter = 0;
$product_array = array();
while ($stmt->fetch()){
    ...store variables
    $counter++;
}
if($counter > 0){
    echo json_encode($product_array);
}else{
    echo json_encode(0);
}
}

问题是当我插入从 HTML 传递的 POST 变量时,我没有正确准备 MySQL 字符串。如果我只为 MySQL 编写以下内容,我测试并确认它可以工作:

"SELECT product.id, product.image_thumb, product.title,
 product.eng_title, product.price, seller.shop_name, seller.id FROM product 
 INNER JOIN seller ON product.seller_id=seller.id WHERE product.active= 'y' 
AND product.deleted= 'n' ORDER BY
 page_views DESC LIMIT 0,12"

我应该如何正确编写初始 HTML 调用,以便在 MySQL 中获得所需的结果?

4

2 回答 2

1

您的主机可能有一个古老的 PHP 功能,称为magic_quotes启用,它会导致所有 GET/POST/etc 数据自动转义。这是(一种非常糟糕的)防止 SQL 注入的方法,现在已被弃用。

你的脚本有一个明显的 SQL 注入漏洞,这个特性确实在“保护”你免受它的侵害。重写您的脚本,以便不会将任何用户输入按原样添加到 SQL 字符串中 - 最好使用参数化查询(google it)。

于 2012-12-30T18:39:53.867 回答
1

我想提请您注意$mysql_order受 SQL 注入的影响。

仅仅因为您使用准备好的语句并不意味着查询是安全的。您需要为$mysql_order part.

于 2012-12-30T18:43:11.273 回答