好吧,這是我第一個使用PHP和MySQL的項目,我認爲使用HTML,PHP和MySQL的Facemash風格的網站將是一個很好的開始。從圖像超鏈接運行PHP函數(Facemash項目)
除了像圖片超鏈接調用「updateHits」函數的行爲不像我所期望的那樣,一切正常。
我相信MySQL數據庫能夠正常運行並且圖片按預期顯示。我的研究指向使用iFrames,jQuery或AJAX來更新「點擊」字段,儘管我無法理解如何在這裏應用它們。
我希望我的代碼可讀,任何建議將不勝感激!
<html>
<body>
<?php
// Make a MySQL Connection
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
mysql_select_db("facemash") or die(mysql_error());
// Select two random people
$personA = rand(1, 28);
$personB = rand(1, 28);
// Ensure that it is not the same person
if ($personB == $personA) {
$personB = rand(1, 28);
}
// Function to return path of photo
function photoPath ($person){
$query = mysql_query("SELECT photo FROM people WHERE id=$person");
$result = mysql_fetch_row($query);
$result = $result[0];
echo $result;
}
// Function to update the hits field
function updateHits($person){
$query = mysql_query("SELECT hits FROM people WHERE id=$person;");
$result = mysql_fetch_row($query);
$result = $result[0];
$result++;
mysql_query("UPDATE people SET hits = $result WHERE id=$person");
}
?>
<!--Image for personA-->
<a href="<?php updateHits($personA);?>"><img src="<?php photoPath($personA);?>"/></a>
<!--Image for personB-->
<a href="<?php updateHits($personB);?>"/><img src="<?php photoPath($personB);?>"/></a>
</body>
</html>
謝謝。
你期待什麼? – ITroubs 2011-04-17 23:43:14