通过PHP在MySQL数据库请求后更新XML文件

时间:2014-01-09 17:19:18

标签: php sql xml mysqli

我有一个读取和打印XML的客户端前端。我有一个管理员前端,通过PHP& amp;读取和写入MySQL数据库。 MySQLi脚本。目前XML和MySQL数据库不绑定。每次操作MySQL数据库后,如何更新或重写XML文件?下面是我的'create.php'文件,它在我的'ajax_demo'表中创建了新的SQL记录。

    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);

但我无法弄清楚如何绑定这两个脚本。有人可以帮我绑定这两个脚本,以便它们相互协调吗?感谢。

编辑 - 1/9/14 7:54 PM

  • 新解决方案:

    使用另一个名为'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>";
      }
      ?>
    

2 个答案:

答案 0 :(得分:1)

 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();
}
?>

答案 1 :(得分:1)

我添加了另一个答案,因为它可能很长: - )

好的,如果您这样做,只需在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); 

}

之后,您需要在需要时调用该函数,并且在INSERT查询后需要该函数,如果查询正确结束,则需要以这种方式修改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();
}
?>