2014-01-09 80 views
1

我有一個讀取和打印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>"; 
        } 
        ?> 
    
+1

包裹在一個函數the'create.php'腳本的功能,包括在其他的腳本文件,然後'newfunction()'調用碼。 –

+0

我可以在一個文件中只有兩個腳本嗎?如果是這樣如何?謝謝! – Jim22150

+1

可以將xml的更新放入其他文件的INSERT查詢的if語句中。 – maurelio79

回答

1

我添加其他答案,因爲也許它可以是長:-)

好吧,如果你這樣做,你只是包括toXml.phpcreate.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(); 
} 
?> 
+0

哇@ maurello79你真棒。我感覺真的回來了,希望這不會讓你長久,但我發現我對這一切都是錯誤的,如果你看到我的整個框架,可能會有幫助,但有一個名爲pre.php的'read.php'文件併發布SQL請求。我通過修改發現了一個非常簡單的解決方案。我在我的問題中發佈了腳本。 – Jim22150

+0

我向你提出了一個pos投票,但是再次感謝! – Jim22150

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

真棒@maurelio,謝謝。但是,我決定採用marc的想法。我怎樣才能使用兩個單獨的文件?我是PHP的新手,類和方法對我來說似乎很奇怪,與我的Java背景相比,請原諒我。 – Jim22150

+1

您在另一個文件中寫入函數update_xml(),並將該文件包含在主文件中 – maurelio79

+0

我在做這篇文章之前曾嘗試過,但是從未創建xml文件。我的語法有什麼問題嗎? – Jim22150