I have a client front-end that reads and prints XML. I have an admin front-end that reads and writes to a MySQL database via PHP & MySQLi script. Currently the XML and MySQL database are not bind. How can I update or rewrite the XML file after each time the MySQL database is manipulated? Below is my 'create.php' file that creates new SQL records within my 'ajax_demo' table.
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();
}
Here is the file 'toXml.php' which I'm trying to bind:
$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);
But I can't figure out how to bind the two scripts. Can someone please help me bind these two scripts so they coordinate with each other? Thanks.
EDIT - 1/9/14 7:54PM
NEW SOLUTION:
used another php file called 'read.php'. I made the XML write script within it instead.
<?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>"; } ?>
create.phpscript in a function, include the file in your other script, thennewfunction()to call the code.include('create.php')would work just as well, except that you might overwrite your previous DB connections, since create.php includes the db libs unconditionally.