通过PHP在MySQL数据库请求后更新XML文件
我有一个读取和打印XML的客户端前端。我有一个管理员前端,通过PHP & MySQLi脚本读写MySQL数据库。目前XML和MySQL数据库没有绑定。每次操作MySQL数据库后,如何更新或重写XML文件?下面是我的'create.php'文件,它在我的'ajax_demo'表中创建新的SQL记录。通过PHP在MySQL数据库请求后更新XML文件
include 'libs/db_connect.php';
include 'toXml.php';
try{
$query = "INSERT INTO ajax_demo SET firstname = ?, lastname = ?";
$stmt = $con->prepare($query);
$stmt->bindParam(1, $_POST['firstName']);
$stmt->bindParam(2, $_POST['lastName']);
if($stmt->execute()){
echo "Person was created.";
}else{
echo "Unable to create person.";
}
}
catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
这里是我想要绑定文件“toXml.php”:
$myFile = "data.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$data_txt .= '<?xml version="1.0" encoding="utf-8"?>';
$data_txt .= '<pages>';
$query = mysql_query("SELECT * FROM ajax_demo");
while($values_query = mysql_fetch_assoc($query))
{
$data_txt .= '<item>';
$data_txt .= '<firstName>' .$values_query['firstName']. '</firstName>';
$data_txt .= '<lastName>' .$values_query['lastName']. '</lastName>';
$data_txt .= '</item>';
}
$data_txt .= '</pages>';
fwrite($fh, $data_txt);
fclose($fh);
但我无法弄清楚如何将两个脚本绑定。有人能帮我把这两个脚本绑定起来,让他们相互协调吗?谢谢。
编辑 - 14年1月9日下午7时54分
-
新的解决方案:
使用所谓的 'read.php' 另一个PHP文件。我在其中创建了XML写脚本。
<?php include 'libs/db_connect.php'; $query = "SELECT id, firstName, lastName, age FROM ajax_demo ORDER BY id asc"; $stmt = $con->prepare($query); $stmt->execute(); $num = $stmt->rowCount(); if($num>0){ echo "<table id='tfhover' class='tftable' border='1'>"; $myFile = "data.xml"; $fh = fopen($myFile, 'w') or die("Can't open XML file."); $data_txt .= '<?xml version="1.0" encoding="utf-8"?>'; $data_txt .= '<pages>'; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>Age</th>"; echo "<th style='text-align:center;'>Action</th>"; echo "</tr>"; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ extract($row); echo "<tr>"; $data_txt .= '<person>'; echo "<td>{$firstName}</td>"; $data_txt .= "<title>{$firstName}</title>"; echo "<td>{$lastName}</td>"; $data_txt .= "<lastName>{$lastName}</lastName>"; echo "</tr>"; $data_txt .= '</person>'; } echo "</table>";//end table $data_txt .= '</pages>'; fwrite($fh, $data_txt); fclose($fh); } else{ echo "<div class='noneFound'>No records found.</div>"; } ?>
我添加其他答案,因为也许它可以是长:-)
好吧,如果你这样做,你只是包括toXml.php
在create.php
并在此开始执行的第一件事是toXml.php
中的内容。这是不好的,因为你需要执行第一create.php
和然后toXml.php
如果包含在另一个文件中的PHP文件,它会被立刻执行,除非你包括文件的内容是一个函数。 所以我的建议是用这种方式创建文件toXml.php
:
function toXml() {
$myFile = "data.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$data_txt .= '<?xml version="1.0" encoding="utf-8"?>';
$data_txt .= '<pages>';
$query = mysql_query("SELECT * FROM ajax_demo");
while($values_query = mysql_fetch_assoc($query))
{
$data_txt .= '<item>';
$data_txt .= '<firstName>' .$values_query['firstName']. '</firstName>';
$data_txt .= '<lastName>' .$values_query['lastName']. '</lastName>';
$data_txt .= '<age>' .$values_query['age']. '</age>';
$data_txt .= '<hometown>' .$values_query['hometown']. '</hometown>';
$data_txt .= '</item>';
}
$data_txt .= '</pages>';
fwrite($fh, $data_txt);
fclose($fh);
}
之后,你需要调用的功能,当你需要,你需要插入查询后,如果查询结束功能正确的,所以你需要修改create.php
这样:
//include database connection
include 'libs/db_connect.php';
/////////////////////////////////
//NEWLY ADDED 1/9/14 3:55PM
include 'toXml.php';
////////////////////////////////
try{
//write query
$query = "INSERT INTO ajax_demo SET firstname = ?, lastname = ?, age = ?, hometown = ?";
//prepare query for excecution
$stmt = $con->prepare($query);
//bind the parameters
//this is the first question mark
$stmt->bindParam(1, $_POST['firstName']);
//this is the second question mark
$stmt->bindParam(2, $_POST['lastName']);
//this is the third question mark
$stmt->bindParam(3, $_POST['age']);
//this is the fourth question mark
$stmt->bindParam(4, $_POST['hometown']);
// Execute the query
if($stmt->execute()){
// If the query is executed correctly now it's the time to launch the function that create the xml
toXml()
echo "Person was created.";
}else{
echo "Unable to create person.";
}
}
//to handle error
catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
?>
update_xml() {
$con = mysqli_connect('localhost','jamestar_user','passed','jamestar_ajax');
$myFile = "rss.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$rss_txt .= '<?xml version="1.0" encoding="utf-8"?>';
$rss_txt .= '<pages>';
$query = mysql_query("SELECT * FROM ajax_demo");
while($values_query = mysql_fetch_assoc($query))
{
$rss_txt .= '<item>';
$rss_txt .= '<firstName>' .$values_query['firstName']. '</firstName>';
$rss_txt .= '<lastName>' .$values_query['lastName']. '</lastName>';
$rss_txt .= '<age>' .$values_query['age']. '</age>';
$rss_txt .= '<hometown>' .$values_query['hometown']. '</hometown>';
$rss_txt .= '<job>' .$values_query['job']. '</job>';
$rss_txt .= '</item>';
}
$rss_txt .= '</pages>';
fwrite($fh, $rss_txt);
fclose($fh);
mysqli_close($con);
}
//include database connection
include 'libs/db_connect.php';
try{
//write query
$query = "INSERT INTO ajax_demo SET firstname = ?, lastname = ?, age = ?, hometown = ?";
//prepare query for excecution
$stmt = $con->prepare($query);
//bind the parameters
//this is the first question mark
$stmt->bindParam(1, $_POST['firstName']);
//this is the second question mark
$stmt->bindParam(2, $_POST['lastName']);
//this is the third question mark
$stmt->bindParam(3, $_POST['age']);
//this is the fourth question mark
$stmt->bindParam(4, $_POST['hometown']);
// Execute the query
if($stmt->execute()){
//if query is ok i update the xml
update_xml();
echo "Person was created.";
}else{
echo "Unable to create person.";
}
}
//to handle error
catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
?>
真棒@maurelio,谢谢。但是,我决定采用marc的想法。我怎样才能使用两个单独的文件?我是PHP的新手,类和方法对我来说似乎很奇怪,与我的Java背景相比,请原谅我。 – Jim22150
您在另一个文件中写入函数update_xml(),并将该文件包含在主文件中 – maurelio79
我在做这篇文章之前曾尝试过,但是从未创建xml文件。我的语法有什么问题吗? – Jim22150
包裹在一个函数the'create.php'脚本的功能,包括在其他的脚本文件,然后'newfunction()'调用码。 –
我可以在一个文件中只有两个脚本吗?如果是这样如何?谢谢! – Jim22150
可以将xml的更新放入其他文件的INSERT查询的if语句中。 – maurelio79