这是我最新的代码,它没有向数据库发送任何值,但没有报告任何错误。
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文件,它大得多,所以我只输了一个条目。
答案 0 :(得分:0)
我不知道您是否只想在该文件中插入最后一个project_ID或all。试试这个,在XML文件中插入每个project_ID。
试试这个:
foreach ($library->message as $message) {
mysql_query("INSERT INTO instancetable (project_ID) VALUES ('" . $message->time_report->project_ID . "')");
}
请注意,所有mysql_ *函数都标记为已弃用。您应该了解PDO。
答案 1 :(得分:0)
$ project_ID未设置。 foreach循环可能会运行,但由于$ project_ID未定义,因此它不会输出任何内容。
如果要插入和打印每个项目ID,则需要在foreach循环中插入查询。
foreach($library->time_report as $project){
mysql_query("INSERT INTO instancetable (project_ID)
VALUES ('{$project->project_ID}')");
printf("project_ID: %s\n", $project->project_ID);
}
请注意,project_ID在此处作为字符串插入。如果数据库字段是整数,则应删除$project->project_ID
周围的单引号。