PHP JSON解析给出错误 [英] PHP JSON parsing giving an error
问题描述
我正在尝试使用PHP解析JSON字符串,使用jQuery $.ajax
以这种格式[{"value":"59"},{"value":"7"},{"value":"46"}]
将JSON发送到PHP文件,但出于某些奇怪的原因,我一直收到此错误"Invalid argument supplied for foreach()"
,在这里是我的PHP和jQuery代码,
I am trying to parse a JSON string using PHP, the JSON gets sent to the PHP file using jQuery $.ajax
in this format, [{"value":"59"},{"value":"7"},{"value":"46"}]
, but for some odd reason I keep getting this error "Invalid argument supplied for foreach()"
, here is my PHP and jQuery code,
jQuery:
$(".systems").each( function(i, system) {
// for each location block
system = $(system);
var sys = {
'value' : $("select[data-prod='products']", system).val()
};
allSystems.push( sys );
});
$.ajax({
type: 'POST',
url: 'systems.php',
dataType: 'json',
data: { systems: JSON.stringify(allSystems), uid: uid },
success: function(data){
alert(data)
}
});
PHP:
require_once 'classes/Zend/Json.php';
$json = $_POST['systems'];
$uid = $_POST['uid'];
$array= Zend_Json::decode("$json");
mysql_connect('localhost','user','pass') or die(mysql_error());
mysql_select_db('products') or die(mysql_error());
//insert the suppliers products into the database
foreach($array as $key){
$query = mysql_query("INSERT INTO suppliersProducts (product_id, supplier_id) VALUES('".$key['value']."', '".$uid."' ) ") or die(mysql_error());
}
print_r($json);
如您所见,我正在使用Zend框架的JSON解码函数来解码传递给PHP的JSON字符串,而且该格式对我来说似乎是正确的,因此我不知道导致此错误的其他原因,也许是编码?有什么想法吗?
As you can see I am using the Zend framework's JSON decode function to decode the JSON string that gets passed to the PHP, and the format seems correct to me so I have no idea what else it could be that is causing this error, maybe encoding? Any ideas?
先谢谢!
推荐答案
看起来json_decode()无法处理该post参数中的数据.在脚本中添加一些错误处理.
Looks like json_decode() couldn't handle the data in that post parameter. Add some error handling to your script.
$test = json_decode($json, true);
if ( is_null($test) ) {
die('data is not encoded as valid json');
}
else if ( !is_array($test) ) {
die('Unexpected data structure. Array expected.');
}
您可能还对 json_last_error()
更新:也许是编码问题.由于您无权访问json_last_error(),因此您可能需要手动"检查字符串和编码,例如
update: Maybe it's an encoding issue. Since you don't have access to json_last_error() you might want to check the string and the encoding "manually", e.g.
if ( is_null($test) ) {
// <--- for debugging purposes only
echo '<pre>Debug: $json=';
var_dump($json);
echo 'hex=';
for($i=0; $i<strlen($json); $i++) {
printf('%02X ', ord($json[$i]));
}
echo '</pre>';
// for debugging purposes only -->
die('data is not encoded as valid json');
在将值作为字符串文字混合到sql语句中时,还必须使用 mysql_real_escape_string() ,请参见 CWE-89:在SQL命令("SQL注入" ')
You also have to use mysql_real_escape_string() when mixing the values as string literals into the sql statement, see CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
$mysql = mysql_connect('localhost','user','password') or die(mysql_error());
mysql_select_db('products', $mysql) or die(mysql_error($mysql));
...
$sql = "
INSERT INTO
suppliersProducts
(product_id, supplier_id)
VALUES
(
'" . mysql_real_escape_string($key['value'], $mysql) . "'
, '" . mysql_real_escape_string($uid, $mysql) . "'
)
";
$query = mysql_query($sql, $mysql) or die(mysql_error($mysql));
甚至更好:使用预先准备好的参数化查询,请参见例如 PDO-准备好的语句和存储过程
Or even better: use prepared, parametrized queries, see e.g. PDO - Prepared statements and stored procedures
这篇关于PHP JSON解析给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!