好吧,这是我第一个使用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