0

这是我最新的代码,它没有向数据库发送任何值,但没有报告任何错误。

 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 文件如上所示,它要大得多,所以我只取出了一个条目。

4

2 回答 2

0

我不知道您是否只想在该文件中插入最后一个 project_ID 或全部。试试这个在你的 XML 文件中插入每个 project_ID。

试试这个:

foreach ($library->message as $message) { 
    mysql_query("INSERT INTO instancetable (project_ID) VALUES ('" . $message->time_report->project_ID . "')");
}

请注意,所有 mysql_* 函数都被标记为已弃用。您应该了解PDO

于 2012-12-07T20:56:00.223 回答
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.

于 2012-12-07T21:18:25.243 回答