Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用AJAX时未正确转义引号_Php_Html_Mysql_Ajax - Fatal编程技术网

Php 使用AJAX时未正确转义引号

Php 使用AJAX时未正确转义引号,php,html,mysql,ajax,Php,Html,Mysql,Ajax,这个javascript/AJAX在我的localhost服务器上工作,但当我将其移动到共享主机时,它现在抛出一个错误,即调用MySQL调用中非对象上的成员函数execute() HTML: onclick="showTrending('page_views DESC', 'product.active= "y" AND product.deleted= "n" ', '12', 'popular')" 然后是javas

这个javascript/AJAX在我的localhost服务器上工作,但当我将其移动到共享主机时,它现在抛出一个错误,即调用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调用,以便在MySQL中获得所需的结果?

您的宿主可能启用了一个名为
magic\u quotes
的古老PHP功能,这会导致所有get/POST/etc数据自动转义。这是一种(非常糟糕的)防止SQL注入的方法,现在已经被弃用了


你的脚本有一个刺耳的SQL注入漏洞,这个特性确实在“保护”你不受它的影响。重写脚本,这样就不会像现在一样将用户输入添加到SQL字符串中—最好是使用参数化查询(google it)。

我想提醒您注意,
$mysql\u order
需要SQL注入

仅仅因为您使用的是prepared语句,并不意味着查询是安全的。
您需要为
$mysql\u订单部分使用白名单

尽管使用了MySQLi编写的语句,但您并没有得到任何好处,事实上,您的脚本仍然容易受到SQL注入的攻击。在HTTP请求中发送SQL代码段以在服务器端构建是个坏主意。因此,仅对这些代码段使用bind_参数而不是直接将其插入bind_结果将解决此问题?@vinsanity38不,它不会解决此问题。不能将任意SQL代码段(如
product.active='y'
)绑定为参数。它必须绑定为
product.active=?
,并且您也不能通过
列名或方向绑定
订单。您真的需要重新思考AJAX如何通知PHP应该将什么放入SQL,而不是通过AJAX发送SQL>ok,我将直接从javascript中删除字符串并输入一些任意数字,这些数字将驱动PHP层中MySQL部分的设置变量,这样用户就没有访问权限,我也不必担心转义字符串:)类似于<代码>数据:{Mysql\u Order:“;drop table users;-”,Mysql\u Limit:Mysql\u Limit,Limit:Limit},
应该可以工作。白名单,如分别传递字段名和值,仔细验证字段名,以及使用参数作为值(你不能参数化一个字段名,但你可以创建一个允许字段的列表并使用它进行验证)这是真的。在他的情况下,他很幸运他犯了一个错误。许多类似的缺陷可能无法被发现,这就是为什么在PDO谈论安全问题之后SQL注入仍然是一个问题的原因之一。这让人大开眼界>
"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"