2011-08-04 22 views
0

我創建了一個模板驅動的網站,但沒有使用像Smarty或Twig這樣的PHP模板包,它完全是我自己的編碼。PHP模板語法 - 這是正確的嗎?

這是一個模板(mypage.php):(移動,以適應頁)

<TITLE>{$title}</TITLE> 
<TABLE> 
<TR><TD>{$maker}</TD><TD>{$model}</TD><TD>{$trim}</TD><TD>{$body}</TD> 
<TD>{$price}</TD> 
</TABLE> 

內容來自MySQL數據庫,就像這樣:

<?php 
mysql_connect("localhost", "mylogin", "password") or die(mysql_error()); 
mysql_select_db("test12") or die(mysql_error()); 
$query = "SELECT name, subject, message FROM contact"; 
$result = mysql_query($query); 

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{ 
    echo "<table><td>{$row['maker']} <br><TD>{$row['model']}</TD><TD>{$row['trim']}</TD><TD>{$row['price']}</TD>" . 

}

我認爲我做得對,但如果我出錯了,讓我知道......這是我第一次正確的去吧! (編輯以適合於stackoverflow.com)

歡呼

+2

什麼是你的問題? – phindmarsh

+0

哪個模板?你在你寫的問題中沒有任何模板,你只是說你想使用PHP。也許你正在尋找[字符串花括號](http://php.net/manual/en/language.types.string.php)(向下滾動到* Variable parsing *)? – hakre

回答

0

一個錯誤,我看到的是,你也沒能逃脫從數據庫中檢索值。
這會讓你開放給XSS。

我會改變回聲部分爲:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{ 
    $maker = htmlspecialchars($row['maker']); 
    $model = htmlspecialchars($row['model']); 
    $trim = htmlspecialchars($row['trim']); 
    $price = htmlspecialchars($row['price']); 

    echo "<table><td>{$maker}<br><TD>{$model}</TD><TD>{$trim}</TD><TD>{$price}</TD>". 
} 
+0

好的,但至於模板...是否正確? – saltwynd8336

+0

@saltwynd我不是一個php模板嚮導,所以不能幫助你。 – Johan