2011-05-10 126 views
0

行 - 我试图为以将其插入到一个名为MySQL表“食谱”如下加载JSON响应(从外部文件名为recipes.json其中包括数以百计的食谱),它的格式为:JSON到MYSQL - JSON响应格式正确 - 循环正确?

{ "recipeName": "After Glow Smoothie", "ingredients": "4 oz. (1/2 cup) pomegranate juice", "ingredients2": "4 oz. (1/2 cup orange juice)", "ingredients3": "2 scoops Vi-Shape shake mix", "ingredients4": "1 cup frozen pineapple", "ingredients5": "5 ice cubes"}, 
{ "recipeName": "All Berry Delight",  "ingredients": "8 oz. skim milk", "ingredients2": "2 scoops Vi-Shape shake mix", "ingredients3": "1/4 cup frozen raspberries", "ingredients4": "1/4 cup frozen blackberries", "ingredients5": "1/4 cup frozen strawberries", "ingredients6": "1/4 cup frozen dark cherries", "ingredients7": "5 ice cubes"} 

我不太习惯数组,所以我想知道为什么我不能通过食谱循环并正确插入它们。我的JSON格式不正确,还是我这样的PHP noob,我在我的主代码中犯了错误。供参考如下:

<?php 
header('Content-Type: text/html; charset=utf-8'); 

$hostname_ndb = "localhost"; 
$database_ndb = "test"; 
$username_ndb = "root"; 
$password_ndb = "root"; 
$ndb = mysql_pconnect($hostname_ndb, $username_ndb, $password_ndb) or trigger_error(mysql_error(),E_USER_ERROR); 

$url = "http://localhost:8888/shakerecipes/recipes.json"; 


$json = file_get_contents($url); 
// var_dump(json_decode($json, true)); 
$out = json_decode($json, true); 


foreach($out["recipeName"] as $recipeNames) { 
$name = addslashes($recipeNames[recipeName]); 
$ingredients= addslashes($recipeNames[ingredients]); 
$ingredients2 = addslashes($recipeNames[ingredients2]); 
$ingredients3 = addslashes($recipeNames[ingredients3]); 
$ingredients4 = addslashes($recipeNames[ingredients4]); 
$ingredients5 = addslashes($recipeNames[ingredients5]); 
$ingredients6 = addslashes($recipeNames[ingredients6]); 
$ingredients7 = addslashes($recipeNames[ingredients7]); 
$ingredients8 = addslashes($recipeNames[ingredients8]); 
$ingredients9 = addslashes($recipeNames[ingredients9]); 

mysql_query("INSERT INTO test (recipeName, ingredients, ingredients2, ingredients3, ingredients4, ingredients5, ingredients6, ingredients7, ingredients8, ingredients9) VALUES('$name', '$ingredients', '$ingredients2', '$ingredients3', '$ingredients4', '$ingredients5', '$ingredients6', '$ingredients7', '$ingredients8', '$ingredients9')") or die (mysql_error()); 
} 
?> 

感谢您的任何和所有提示/帮助。

BRR

+3

1. addslashes的不是你如何防范注射。 addslashes是一个湿式的洗手间,填满了胡佛水坝的50英尺裂缝。 2.如果你不知道你从json_decode调用中得到了什么,你可以var_dump/print_r并看看。 – 2011-05-10 01:27:09

回答

0

对于那些谁不知道什么工作 - 这里是我想出了:

<?php 
header('Content-Type: text/html; charset=utf-8'); 

$hostname_ndb = "localhost"; 
$database_ndb = "test"; 
$username_ndb = "root"; 
$password_ndb = "root"; 
$ndb = mysql_pconnect($hostname_ndb, $username_ndb, $password_ndb) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db($database_ndb); 

$url = "http://localhost:8888/shakerecipes/recipes.json"; 
$json = file_get_contents($url); 

$out = json_decode($json, true); 

foreach($out["recipes"] as $recipe) { 
$name = addslashes($recipe[recipeName]); 
$ingredients= addslashes($recipe[ingredients]); 
$ingredients2 = addslashes($recipe[ingredients2]); 
$ingredients3 = addslashes($recipe[ingredients3]); 
$ingredients4 = addslashes($recipe[ingredients4]); 
$ingredients5 = addslashes($recipe[ingredients5]); 
$ingredients6 = addslashes($recipe[ingredients6]); 
$ingredients7 = addslashes($recipe[ingredients7]); 
$ingredients8 = addslashes($recipe[ingredients8]); 
$ingredients9 = addslashes($recipe[ingredients9]); 

mysql_query("INSERT INTO recipes (recipeName, ingredients, ingredients2, ingredients3, ingredients4, ingredients5, ingredients6, ingredients7, ingredients8, ingredients9) VALUES('$name', '$ingredients', '$ingredients2', '$ingredients3', '$ingredients4', '$ingredients5', '$ingredients6', '$ingredients7', '$ingredients8', '$ingredients9')") or die (mysql_error()); 

} 
?> 
1

首先,你应该使用mysql_real_escape_string,而不是和addslashes的。

其次你应该/可以预制另一个foreach循环与$ recipeNames。

或者你可以做到lambda /封闭风格。

array_walk($recipeNames, function(&$value) { 
    $value = mysql_real_escape_string($value); 
}); 

之后,你可以爆你的价值观

mysql_query("INSERT INTO test (recipeName, ingredients, ingredients2, ingredients3, ingredients4, ingredients5, ingredients6, ingredients7, ingredients8, ingredients9) VALUES('".implode('\',\'', $recipeNames)."')") or die (mysql_error()); 
+0

这是一个脚本,旨在运行一次填充数据库,所以我不认为安全是必要的。我试着循环根据你的代码,并收到以下内容:“array_walk()期望参数1是数组,空给出....”任何其他的想法? – 2011-05-10 02:32:51

+0

对于那些想知道什么工作的人 - 这是我想出的: – 2011-05-17 14:43:47