JSON 到 MYSQL - JSON 响应格式是否正确 - 正确循环?

发布于 2024-11-05 21:40:18 字数 2290 浏览 0 评论 0原文

好的 - 我正在尝试加载 JSON 响应(来自名为recipes.json 的外部文件,其中包含数百个食谱),其格式如下,以便将其插入名为“recipes”的 MySQL 表中:

{ "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 菜鸟,以至于我在主代码中犯了错误。参考如下:

<?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

OK - I am attempting to load a JSON response (from an external file called recipes.json which includes hundreds of recipes) which is formatted as follows in order to insert it into a MySQL table called "recipes":

{ "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"}

I am not too handy with arrays so I am wondering why I am not able loop through the recipes and insert them properly. Is my JSON malformed or am I such a PHP noob that I am making mistakes in my main code. For reference it as follows:

<?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()); 
}
?>

Thanks for any and all tips/help.

BRR

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-11-12 21:40:18

首先,您应该使用mysql_real_escape_string而不是addslashes 。

其次,您应该/可以使用 $recipeNames 执行另一个 foreach 循环。

或者你可以使用 lambda/closure 风格。

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());

To begin with, you should use mysql_real_escape_string instead of addslashes.

Secondly you should/could preform another foreach loop with $recipeNames.

Or you can do it lambda/closure style.

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

Afterward you can implode your values

mysql_query("INSERT INTO test (recipeName, ingredients, ingredients2, ingredients3, ingredients4, ingredients5, ingredients6, ingredients7, ingredients8, ingredients9) VALUES('".implode('\',\'', $recipeNames)."')") or die (mysql_error());
横笛休吹塞上声 2024-11-12 21:40:18

对于那些想知道什么有效的人 - 这是我想出的:

<?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()); 

}
?>

For those who wonder what worked - here is what I came up with:

<?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()); 

}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文