gpt4 book ai didi

php - 无法插入 MySQL 数据库

转载 作者:行者123 更新时间:2023-12-01 00:30:52 24 4
gpt4 key购买 nike

我有几个 xml 文件。我正在尝试将从这些 xml 文件中收集的数据插入到数据库中。问题是有些被插入而有些没有。这是两个 xml 文件。第一个插入时没有任何pb,但是第二个没有插入到数据库中。

<?xml version="1.0" encoding="UTF-8"?>

<recipeml version="0.5">
<recipe>
<head>
<title>10 Minute Meat Loaf</title>
<categories>
<cat>Beef</cat></categories>
<yield>4</yield></head>
<ingredients>
<ing>
<amt>
<qty>1</qty>
<unit>pound</unit></amt>
<item>Ground beef</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit/></amt>
<item>Egg</item></ing>
<ing>
<amt>
<qty>1/2</qty>
<unit>cups</unit></amt>
<item>Bread crumbs</item></ing>
<ing>
<amt>
<qty>1/4</qty>
<unit>cups</unit></amt>
<item>Milk</item></ing>
<ing>
<amt>
<qty>2</qty>
<unit>tablespoons</unit></amt>
<item>Onion soup mix</item></ing>
<ing>
<amt>
<qty>2</qty>
<unit>tablespoons</unit></amt>
<item>Ketchup</item></ing>
<ing>
<amt>
<qty>2</qty>
<unit>tablespoons</unit></amt>
<item>Soy sauce</item></ing>
<ing>
<amt>
<qty>1/2</qty>
<unit>cups</unit></amt>
<item>Swiss cheese; shredded</item></ing></ingredients>
<directions>
<step> Combine all ingredients and shape into a round or oval loaf. Place in
microwave-safe dish, cover with waxed paper and microwave on high for 10
minutes, turning dish after 5 minutes, of cooking, drain and cover with
foil. Let stand 10 minutes before slicing. Makes 4-6 servings.

busted by sooz

Recipe by: Key Gourmet

Posted to recipelu-digest by sooz &lt;kirkland@gj.net&gt; on Mar 18, 1998

</step></directions></recipe></recipeml>

****我无法将第二个 XML 文件插入数据库***

<?xml version="1.0" encoding="UTF-8"?>

<recipeml version="0.5">
<recipe>
<head>
<title>10 Can Chicken Casserole</title>
<categories>
<cat>Casserole</cat></categories>
<yield>8</yield></head>
<ingredients>
<ing>
<amt>
<qty>1</qty>
<unit>large</unit></amt>
<item>Fryer; boiled with onion and celery; deboned, cut up</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>Cheddar cheese soup</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>Cream of mushroom soup</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>(small) evaporated milk</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>(#202) asparagus tips; drained</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>French style green beans; drained</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>Water chestnuts; drained, sliced</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>(4-oz) mushroom stems &amp; pieces with liquid</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>(small) pimento; diced, with liquid</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>Chow mein noodles</item></ing>
<ing>
<amt>
<qty>1</qty>
<unit>can</unit></amt>
<item>French fried onion rings</item></ing>
<ing>
<amt>
<qty/>
<unit/></amt>
<item>Red pepper to taste</item></ing></ingredients>
<directions>
<step> Mix all ingredients, except the onion rings, together. Put into a 3 quart
casserole; top with onion rings. Bake at 350 degrees until bubbly,
approximately 30 minutes.

From &lt;A Taste of Louisiana&gt;.Downloaded from Glen's MM Recipe Archive,
http://www.erols.com/hosey.

</step></directions></recipe></recipeml>

现在,这是我在 php5 中的代码:

<?php

$link = mysql_connect('localhost', 'root', 'recipe');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db('php_database');

$sql = 'DROP TABLE IF EXISTS `recipe`';
mysql_query( $sql, $link );

$sql = 'CREATE TABLE `recipe` (
`idRecipe` TINYINT( 12) UNSIGNED NOT NULL AUTO_INCREMENT ,
`RecipeTitle` VARCHAR( 75 ) NOT NULL,
`Yield` TINYINT( 5) UNSIGNED NOT NULL ,
`Steps` Text( 10000 ) NOT NULL,

PRIMARY KEY ( `idRecipe` )
)';
echo 'Creating table: \'recipe\'....';
mysql_query( $sql, $link );



if ($handle = opendir('C:\wamp\www\PhP\Folder xml'))
{


while (false !== ($file = readdir($handle)))
{ //$i++;

if ($file != '.' && $file != '..')
{



if (file_exists($file))
{

$use_errors = libxml_use_internal_errors(true);
$xml = simplexml_load_file($file);
if (!$xml)
{
echo ("Cannot open the XML file $file <br />");

}
else
{

echo "<br /> <br />";

$title=$xml->recipe->head->title;
$yield=$xml->recipe->head->yield;
$step=$xml->recipe->directions->step;

echo "value of Recipe Title= $title, yield= $yield and step= $step<b />";
mysql_query("INSERT INTO recipe (RecipeTitle,Yield,Steps) values ('$title', '$yield','$step')");
printf("Last inserted record has id %d\n", mysql_insert_id());
mysql_close($link);
echo "<br />";

$xmla= "$file";
$xmlDoc = new DOMDocument();
$xmlDoc->load($xmla);
$cat = $xmlDoc->getElementsByTagName("cat");
$numOfcat = $cat->length;
echo "@@ here is the number of cat :$numOfcat @@<br />";

$qty = $xmlDoc->getElementsByTagName("qty");
$numOfqty = $qty->length;
echo "@@ here is the number of qty :$numOfqty @@<br />";


echo $xml->getName() . "<br />"; // displays recipeml

foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />"; //displays recipe:
//echo "INSERT " . $child . " INTO....";

echo ("title= ");
echo $xml->recipe->head->title . " ,\r\n" ; //displays the title
echo ("category= ");
echo $xml->recipe->head->categories->cat . ",\n" ;// displays the category
echo ("yield= ");
echo $xml->recipe->head->yield . ",\n" ; //displays yield number
echo ("quantity= ");
echo $xml->recipe->ingredients->ing->amt->qty . ",\n" ; //displays quantity
echo ("unit= ");
echo $xml->recipe->ingredients->ing->amt->unit . ",\n" ; ////displays unit
echo ("Item= ");
echo $xml->recipe->ingredients->ing->item . ",\n" ; //displays item

for($i=1; $i<$numOfqty; $i=$i+1)
{
echo ("quantity= ");
echo $xml->recipe->ingredients->ing[$i]->amt->qty . ",\n" ;
echo ("unit= ");
echo $xml->recipe->ingredients->ing[$i]->amt->unit . ",\n" ;
echo ("Item= ");
echo $xml->recipe->ingredients->ing[$i]->item . ",\n" ;
}
echo ("Step to cook=********************** ");
echo $xml->recipe->directions->step . ",\n" ; //displays item
}
}
}
}
}
}

closedir($handle);
?>

有什么建议吗?其中许多文件格式不正确,但我使用 Altova 检查这两个 xml 文件是否格式正确,altova 确认它们都格式正确。我将不胜感激任何建议。

谢谢,

T3000。

最佳答案

您在第二个方向->步骤中有一个 ',这将使查询失败,因为 ' 之后的其余文本将被视为常规 SQL - 而事实并非如此。

你应该使用 mysql_real_escape_string在将它们插入查询之前在字符串上。

或者切换到MySQLi或使用准备好的语句的数据库抽象层。

关于php - 无法插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6392751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com