我有一個讀取和打印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>"; } ?>
包裹在一個函數the'create.php'腳本的功能,包括在其他的腳本文件,然後'newfunction()'調用碼。 –
我可以在一個文件中只有兩個腳本嗎?如果是這樣如何?謝謝! – Jim22150
可以將xml的更新放入其他文件的INSERT查詢的if語句中。 – maurelio79