Jump to content

PHP/XML/MySQL problem...


davejog

Recommended Posts

 

Hi, new here, Dave is the name.

 

I have this problem:

 

every x minutes I have an XML which I save on local path, and I use new SimpleXMLElement() to parse it.

 

XML sample:

<?xml version="1.0" encoding="utf-8"?>
<all_cars>
	<car>
		<color>silver</color>
		<year>2008</year>
		<price>23000</price>
		<model>Focus</model>
		<company>Ford</company>
	</car>
	<car>
		<color>black</color>
		<year>2007</year>
		<price>20000</price>
		<model>Focus</model>
		<company>Ford</company>
	</car>
</all_cars>

 

 

now, I do a foreach loop like this:

foreach($xml->cars->car as $car) {
// bla bla
}

 

there is no problem with the DB con, I tested it, for some reason, it seems to fail to insert the query...

I want to insert into DB, so I created this:

$res = mysql_query("INSERT INTO tmp_test VALUES ({$car->color},{$car->year},{$car->price},{$car->model},{$car->company})",$hd)
or die ("Unable to run cars insert query");

 

it keeps returning "Unable to run cars insert query"... again, the problem is in how I put

{$car->year}

and such in the query...

 

anyone??

Link to comment
Share on other sites

Or even better create the query as a variable so you can echo it out to the page if it fails!

 

$query = "INSERT INTO tmp_test VALUES ('{$car->color}','{$car->year}','{$car->price}','{$car->model}','{$car->company}');";

$res = mysql_query($query, $hd)
       or die ("Unable to run cars insert query:<br />$query<br />Error:<br />".mysql_error());

Link to comment
Share on other sites

yup, I got that, but where exactly do I use it in the code? how do I insert this in the php page?

 

<?php
$str = "<?xml version='1.0' encoding='utf-8'?>
<all_cars>
	<car>
		<color>silver</color>
		<year>2008</year>
		<price>23000</price>
		<model>Focus</model>
		<company>Ford's</company>
	</car>
	<car>
		<color>black</color>
		<year>2007</year>
		<price>20000</price>
		<model>Focus</model>
		<company>Ford's</company>
	</car>
</all_cars>";

$xml = simplexml_load_string($str);

foreach ($xml->car as $car)
{
    $color = addslashes($car->color);
    $year = addslashes($car->year);
    $price = addslashes($car->price);
    $model = addslashes($car->model);
    $company = addslashes($car->company);
    
    $sql = "INSERT INTO tmp_test VALUES ('$color','$year','$price','$model','$company')";
    echo $sql, '<br>';
#    $res = mysql_query($sql) or die ("Unable to run cars insert query");
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.