这是我最新的代码,它没有向数据库发送任何值,但没有报告任何错误。
foreach($library->time_report as $project){
mysql_query("INSERT INTO instancetable (project_ID, project_status, client_ID, project_start_time, project_end_time, project_total_time, employee_ID, employee_name, date_created, date_modified, created_by, client_name, priority, organization, number_of_required_types)
VALUES ('{$project->project_ID}',{$project->project_status}, '{$project->client_ID}', {$project->project_start_time}, '{$project->project_end_time}', '{$project->project_total_time}','{$project->employee_ID}', {$project->employee_name}, {$project->date_created}, {$project->date_modified}, {$project->created_by}, {$project->client_name}, {$project->priority}, {$project->organization}, {$project->number_of_required_types}");
}
我需要将一个 XML 文件上传到 mysql 中。它说它正在成功上传,但它只是在表中放置了一个值 0。这是我的代码:
<?php
echo "starting <br><br>";
//mysql connection
$con2 = mysql_connect("localhost","test","test");
if (!$con2) { die('Could not connect: ' . mysql_error()); }
$selectdb = mysql_select_db("test", $con2);
if (!$selectdb) { die('Database not used: ; ' . mysql_error()); }
echo "connected to DB<br/><br/>";
//simplexml load xml file
$library = simplexml_load_file('http://localhost/instance.xml');
echo "xml loaded<br/><br/>";
//loop through parsed xmlfeed and print output
foreach ($library->message as $message) {
printf("project_ID: %s\n", $project_ID->project_ID);
}
echo "xml parsed<br/><br/>";
//insert into databse
mysql_query("INSERT INTO instancetable (project_ID)
VALUES ('project_ID')")
or die(mysql_error());
echo "inserted into mysql<br/><br/>";
//show updated records
printf ("Records inserted: %d\n", mysql_affected_rows());
//close connection
mysql_close($con2);
echo " <br /> Finished "
?>
所以这是我得到的输出:
starting
connected to DB
xml loaded
xml parsed
inserted into mysql
Records inserted: 1
Finished
XML 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project>
<time_report>
<project_ID>4</project_ID>
<project_status>Closed</project_status>
<client_ID>6001</client_ID>
<time_record>
<project_start_time>13:03:19.000</project_start_time>
<project_end_time>13:14:26.000</project_end_time>
<project_total_time>0.0104166666666667</project_total_time>
</time_record>
<employee_ID>10001</employee_ID>
<employee_name>Mary Beth</employee_name>
<date_created>2009-07-02</date_created>
<date_modified>2009-11-24</date_modified>
<created_by>Mary Beth</created_by>
<client_name>BlackRock Funds</client_name>
<priority/>
<organization/>
<number_of_required_types>9999999999</number_of_required_types>
</time_report>
XML 文件如上所示,它要大得多,所以我只取出了一个条目。