PHP版本:5.3.19 MYSQL版本:5.0.96如何更新HTML代碼到MySQL場
我有一個數據庫表包含XML數據的字段:
XML數據:
<?xml version="1.0"?>
<book>
<page>
<data>Page 1</data>
</page>
<page>
<data>Page 2</data>
</page>
<page>
<data>Page 3</data>
</page>
</book>
我已經成功了xml數據顯示,現在我想用PHP來更新它
PHP代碼:
$id = 1;
$fav_id = $id;
$userid = 1;
$data_update = '<p style="text-align: center;"><span style="text-decoration: underline; font-size: xx-large;"><em><strong>Title</strong></em></span></p>';
$page_number = 0;
mysql_select_db($database_main, $main);
$query_Recordset1 = "SELECT * FROM projects WHERE id = $id";
$Recordset1 = mysql_query($query_Recordset1, $main) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$xmldata = (string)$row_Recordset1['projectData'];
$parsed = simplexml_load_string($xmldata);
$data = $parsed->xpath('//data');
$data[$page_number] = $data_update;
$x = 0;
$array_length = count($data);
while($x < $array_length){
if($x==0){
$finalData .= '<?xml version="1.0"?><book>';
}
$finalData .= "<page><data>$data[$x]</data></page>";
if($x==$array_length-1){
$finalData .= "</book>";
}
$x++;
}
$FinalData = mysql_real_escape_string($finalData);
$updateSQL = "UPDATE projects SET projectData=$finalData WHERE id=$fav_id";
mysql_select_db($database_main, $main);
$Result1 = mysql_query($updateSQL, $main) or die(mysql_error());
我想最終的結果是:
<?xml version="1.0"?>
<book>
<page>
<data>
<p style="text-align: center;">
<span style="text-decoration: underline; font-size: xx-large;">
<em><strong>Title</strong></em>
</span>
</p>
</data>
</page>
<page>
<data>Page 2</data>
</page>
<page>
<data>Page 3</data>
</page>
</book>
雖然當我嘗試addslashes()
和mysql_real_escape_string()
我得到:
DATA:
<p center;\\\"="" style="\\\"text-align:">
<span xx-large;\\\"="" font-size:="" underline;="" style="\\\"text-decoration:">
<em>
<strong>Title</strong>
</em>
</span>
</p>
我的問題是,爲什麼那些兩個函數[addslashes()
和mysql_real_escape_string()
]給了我上面的結果,以及如何防止它發生NG?