[英]php to insert simple xml into mysql db tables with foreign key
Having a real issue with logic for inserting xml elements into mysql db with php. 对于使用php将xml元素插入mysql db的逻辑存在实际问题。 I have found ALL examples only ever use 1 table (easy). 我发现所有示例仅使用1个表(简单)。 i need to have a table for property elements and then a table to hold the many pictures that are nested another level in on the xml file. 我需要有一个用于属性元素的表,然后一个表来保存嵌套在xml文件中另一层的许多图片。
The pictures need to have a foreign key related to the individual property_id . 图片需要具有与单个property_id相关的外键。 I managed to do this by using insert_last_id but it only inserted 1 picture.. not the many that are attached under the element . 我设法通过使用insert_last_id做到了这一点,但它只插入了1张图片..而不是元素下面附加的许多图片。 ( which is a url not a jpeg) so just a varchar column. (这是一个网址,而不是jpeg),因此只是一个varchar列。 ) )
Any help be grateful. 任何帮助将不胜感激。 (prepared statements will be used, i just started like this) (将使用准备好的语句,我就是这样开始的)
//loads in xml //载入xml
$xml = simplexml_load_file('propertieshort.xml') or die("can not find it"); $ xml = simplexml_load_file('propertieshort.xml')或die(“找不到它”);
//loops through each property and inserts selected $variables. //循环遍历每个属性并插入选定的$ variables。
foreach($xml->branches->branch->properties->property as $row){
$price_text = (string)$row->numeric_price;
$bedrooms = (int)$row->bedrooms;
$priority =(string)$row->priority;
$advert_heading = (string)$row->advert_heading;
$brochure = (string)$row->brochure;
$sql = "INSERT INTO properties( price_text, bedrooms, priority,
advert_heading, brochure) VALUES ('". $price_text ."','". $bedrooms ."','".
$priority ."','". $advert_heading ."','". $brochure ."')";
//playing around with this but no luck linking property_id to many pictures $prop_id = mysqli_insert_id($conn); //玩这个,但没有运气将property_id链接到许多图片。$ prop_id = mysqli_insert_id($ conn);
//trying to loop the many pictures associated with each property foreach ($xml->branches->branch->properties->property->pictures->picture as $rows) //尝试循环与每个属性foreach关联的许多图片($ xml->分支->分支-> properties->属性->图片->图片为$ rows)
$pictures = (string)$rows->filename;
$sql2 = "INSERT INTO pictures(property_id, filename) VALUES ('". $prop_id
."' , '". $pictures ."')";
//i just print to html for testing not insert into db
print_r($sql);
echo"<br><br>", print_r($sql2);echo"<br><br>";
<?xml version="1.0" encoding="ISO-8859-1" ?>
<agency>
<branches>
<branch name=" Baxters Bath">
<properties>
<property reference="BB00000285">
<availableFrom>02/12/2011 00:00:00</availableFrom>
<department>Agricultural</department>
<propertyofweek>NO</propertyofweek>
<price_text>OIRO £749,950</price_text>
<numeric_price>749950.0000</numeric_price>
<bedrooms>4</bedrooms>
<receptions>2</receptions>
<bathrooms>0</bathrooms>
<priority>Exchanged</priority>
<tenure>Leasehold</tenure>
<advert_heading>A superbly presented four bedroom two bathroom semi detached family house.</advert_heading>
<main_advert>A superbly presented four bedroom two bathroom semi detached family house. The property has been extended to offer large ground floor accomodation and also benifits from a workshop/ office at the rear of the garden.
<a href="http://www.google.co.uk" target="_blank">this is a link</a></main_advert>
<advert2>A superbly presented four bedroom two bathroom semi detached family house. The property has been extended to offer large ground floor accomodation and also benifits from a workshop/ office at the rear of the garden.
<a href="http://www.google.co.uk" target="_blank">this is a link</a></advert2>
<advert3 />
<advert4 />
<advert5 />
<advert6 />
<house_number>11</house_number>
<street>Murhill</street>
<district>Limpley Stoke</district>
<town>Bath</town>
<county />
<postcode>BA2 7FG</postcode>
<country>UK </country>
<area>Bath City Centre</area>
<bullet1 />
<bullet2 />
<bullet3 />
<bullet4 />
<bullet5 />
<bullet6 />
<bullet7 />
<bullet8 />
<bullet9 />
<bullet10 />
<bullet11 />
<bullet12 />
<bullet13 />
<bullet14 />
<bullet15 />
<bullet16 />
<bullet17 />
<bullet18 />
<bullet19 />
<bullet20 />
<brochure />
<epc />
<hips />
<filename3 />
<filename4 />
<filename5 />
<filename6 />
<virtual_tour_url />
<property_type>House</property_type>
<commercial_type> Dairy Unit</commercial_type>
<property_style>Semi Detached</property_style>
<property_reference>367268</property_reference>
<newHome>NO</newHome>
<noChain>NO</noChain>
<furnished>Unknown</furnished>
<rooms />
<pictures>
<picture name="Front">
<filename>http://www.expertagent.co.uk/in4glestates/{76C20EAB-0589-4AEB-A7BE-CE3182F07285}/{3CCBEC59-BBE0-4DFE-864C-582BDAB5C577}/7A.jpg</filename>
</picture>
<picture name="Dining Room">
<filename>http://www.expertagent.co.uk/in4glestates/{76C20EAB-0589-4AEB-A7BE-CE3182F07285}/{3CCBEC59-BBE0-4DFE-864C-582BDAB5C577}/7B.jpg</filename>
</picture>
<picture name="Garden">
<filename>http://www.expertagent.co.uk/in4glestates/{76C20EAB-0589-4AEB-A7BE-CE3182F07285}/{3CCBEC59-BBE0-4DFE-864C-582BDAB5C577}/7D.jpg</filename>
</picture>
<picture name="Photo 5">
<filename>http://www.expertagent.co.uk/in4glestates/{76C20EAB-0589-4AEB-A7BE-CE3182F07285}/{3CCBEC59-BBE0-4DFE-864C-582BDAB5C577}/HH002070.JPG</filename>
</picture>
</pictures>
<floorplans />
</property>
</properties>
</branch>
</branches>
</agency>
Try this: 尝试这个:
foreach($xml->branches->branch->properties->property as $row){
echo 'Inside Property Loop';
$price_text = (string)$row->numeric_price;
$bedrooms = (int)$row->bedrooms;
$priority =(string)$row->priority;
$advert_heading = (string)$row->advert_heading;
$brochure = (string)$row->brochure;
$allPictures = (array)$row->pictures;
foreach($allPictures['picture'] as $singlePic){
echo '<br/> >>> Inside Picture loop.<br/>';
$name = (string)$singlePic['name'];
$url = (string)$singlePic->filename;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.