I want to insert data form an xml-feed into a mysql database (see code below).
<?php
error_reporting(E_ALL); ini_set('display_errors', 'On');
set_time_limit(0);
$xmlDoc = new DOMDocument();
$xmlDoc->load("daisybelfeed.xml");
$mysql_hostname = "********";
$mysql_user = "*********";
$mysql_password = "*********";
$mysql_database = "*********";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd)
or die("Opps some thing went wrong");
$drop_tabel = "DROP TABLE belvilla";
$create_tabel = "CREATE TABLE belvilla
(title VARCHAR(255), img_medium TEXT, country_of_destination TEXT, region_of_destination TEXT,city_of_destination CHAR(100), latitude FLOAT( 10, 6 ) NOT NULL, longitude FLOAT( 10, 6 ) NOT NULL, link TEXT, image TEXT, minimum_price DECIMAL NOT NULL, maximum_price DECIMAL NOT NULL, stars NUMERIC(1) NOT NULL, max_nr_people NUMERIC(1) NOT NULL);";
$drop = mysql_query($drop_tabel) or die('Error, drop query failed');
$create = mysql_query($create_tabel) or die('Error, create query failed');
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=15000; $i++)
{
$title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$image=$x->item($i)->getElementsByTagName('img_medium')
->item(0)->childNodes->item(0)->nodeValue;
$country=$x->item($i)->getElementsByTagName('country_of_destination')
->item(0)->childNodes->item(0)->nodeValue;
$regio=$x->item($i)->getElementsByTagName('region_of_destination')
->item(0)->childNodes->item(0)->nodeValue;
$city=$x->item($i)->getElementsByTagName('city_of_destination')
->item(0)->childNodes->item(0)->nodeValue;
$latitude=$x->item($i)->getElementsByTagName('latitude')
->item(0)->childNodes->item(0)->nodeValue;
$longitude=$x->item($i)->getElementsByTagName('longitude')
->item(0)->childNodes->item(0)->nodeValue;
$link=$x->item($i)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$vanafprijs=$x->item($i)->getElementsByTagName('minimum_price')
->item(0)->childNodes->item(0)->nodeValue;
$maximaalprijs=$x->item($i)->getElementsByTagName('maximum_price')
->item(0)->childNodes->item(0)->nodeValue;
$sterren=$x->item($i)->getElementsByTagName('stars')
->item(0)->childNodes->item(0)->nodeValue;
$maxpeople=$x->item($i)->getElementsByTagName('max_nr_people')
->item(0)->childNodes->item(0)->nodeValue;
$title=mysql_real_escape_string($title);
$image=mysql_real_escape_string($image);
$link=mysql_real_escape_string($link);
$regio=mysql_real_escape_string($regio);
$city=mysql_real_escape_string($city);
if ($country == 'FR'){
$insert = "INSERT INTO belvilla (title, img_medium, country_of_destination, region_of_destination, city_of_destination, latitude, longitude, link, image, minimum_price, maximum_price, stars, max_nr_people) VALUES ('$title', '$image', '$country', '$regio', '$city' $latitude, $longitude, '$link', '$image', $vanafprijs, $maximaalprijs, $sterren, $maxpeople)";
$add_member = mysql_query($insert) or die("Error, insert query failed Error: (" . mysql_errno() . ") " . mysql_error());
}}
?>
Everything works fine, exept for one issue: I get an mySQL error code 1064 caused by some citynames containing a hyphen (like "La Roche-en-Ardenne"). As you can see in the code I tried to solve this problem using mysql_real_escape_string, but this doesn't work out.
Anyone an idea on how to solve this problem?
Thanks!
I think that hyphens are not the problem.
The problem is a missing comma between '$city'
and $latitude
in
VALUES ('$title', '$image', '$country', '$regio', '$city' $latitude, $longitude, '$link', '$image', $vanafprijs, $maximaalprijs, $sterren, $maxpeople)
在插入命令中,“ $ city”和$ latitude之间没有逗号
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.