我有几个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 <kirkland@gj.net> on Mar 18, 1998
</step></directions></recipe></recipeml>
** * * 第二个XML文件我无法插入数据库< / EM> * **
<?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 & 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 <A Taste of Louisiana>.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。
答案 0 :(得分:4)
你有一个'在你的第二个方向 - &gt;步骤,这将使查询失败,因为'将被视为常规SQL之后的其余文本 - 它不是。
在将字符串插入查询之前,您应该对字符串使用mysql_real_escape_string。
或切换到MySQLi或使用预准备语句的DB抽象层。
答案 1 :(得分:2)
mysql_real_escape_string()将逃避您的数据&amp;使用或死亡将报告失败的地方。基本调试......
$query="INSERT INTO recipe (RecipeTitle,Yield,Steps) values
('".mysql_real_escape_string($title)."',
'".mysql_real_escape_string($yield)."',
'".mysql_real_escape_string($step)."')";
mysql_query($query,$link) or die(mysql_error());
在第一次迭代后关闭连接,然后再次建立链接。
通常不使用mysql_close() 必要的,作为非持久的开放 链接自动关闭 脚本执行结束。
不相关但使用网址友好路径,下划线而不是空格
$handle = opendir('C:\wamp\www\PhP\Folder_xml');