次のスクリプトを使用して、Web サイトで php-mysql 投票システム (YouTube のようなもの) を構築しています。また、newtest.phpでphpを実行するためにajaxを使用しています。私は自分でphpをテストしましたが、うまくいきました。今、私は ajax でマウスのクリックを使用して上下に投票していますが、機能していません。どこが間違っているのか教えていただけますか? (私は ajax の初心者です) . また、自動的に更新された現在の投票数を返して表示したい。
$contentVar = $_POST['contentVar'];
if ($contentVar == "con1") {
$db_host = "host";
$db_username = "username";
$db_pass = "pass";
$db_name = "phphitscounter";
@mysql_connect ("$db_host","$db_username","$db_pass") or die ("Could Not Connect To The Database");
@mysql_select_db ("$db_name") or die ("No Database");
mysql_select_db("phphitscounter", @mysql_connect ("$db_host","$db_username","$db_pass"));
$ip = $_SERVER['REMOTE_ADDR'];
$Page = $_SERVER['PHP_SELF'];
$pagenum = preg_replace("/[^0-9]/", '', $Page);
$query = mysql_query("SELECT ip FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
if (mysql_num_rows($query) > 0) {
echo "You have already voted";
}
else{
echo "Thank you for voring";
mysql_query("INSERT INTO vote (ip, page, voteup, votedown) VALUES ('$ip', '$pagenum', 0, 0)");
mysql_query("UPDATE vote SET voteup = voteup + 1 WHERE ip = '$ip' AND page = '$pagenum'");
}
$voteup = mysql_query("SELECT voteup FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
$results_voteup = mysql_fetch_array($voteup, MYSQL_NUM);
print ($results_voteup[0]);
echo "1";
}
else if ($contentVar == "con2") {
$db_host = "host";
$db_username = "username";
$db_pass = "pass";
$db_name = "phphitscounter";
@mysql_connect ("$db_host","$db_username","$db_pass") or die ("Could Not Connect To The Database");
@mysql_select_db ("$db_name") or die ("No Database");
mysql_select_db("phphitscounter", @mysql_connect ("$db_host","$db_username","$db_pass"));
$ip = $_SERVER['REMOTE_ADDR'];
$Page = $_SERVER['PHP_SELF'];
$pagenum = preg_replace("/[^0-9]/", '', $Page);
$query = mysql_query("SELECT ip FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
if (mysql_num_rows($query) > 0) {
echo "You have already voted";
}
else {
echo "Thank you for voring";
mysql_query("INSERT INTO vote (ip, page, voteup, votedown) VALUES ('$ip', '$pagenum', 0, 0)");
mysql_query("UPDATE vote SET votedown = votedown + 1 WHERE ip = '$ip' AND page = '$pagenum'");
}
$votedown = mysql_query("SELECT voteup FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
$results_votedown = mysql_fetch_array($votedown, MYSQL_NUM);
print ($results_votedown[0]);
echo "2";
}
test.php ページ:
<html>
<head>
<script type="text/javascript" src="Scripts/jquery.js"></script>
<script language="JavaScript" type="text/javascript">
function swapContent(cv) {
$("#myDiv").html('<img src="Assets/loading.gif" width="50" height="50"/>').show();
var url = "scripts/newtest.php";
$.post(url, {
contentVar: cv
}, function(data) {
$("#myDiv").html(data).show();
});
}
</script>
<style type="text/css">
#myDiv {
width: 200px;
height: 150px;
padding: 12px;
border: #666 1px solid;
background-color: #FAEEC5;
font-size: 18px;
}
</style>
</head>
<body>
<img src="Assets/like.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con1');" />
<img src="Assets/dislike.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con2');" />
<div id="myDiv"></div>
</body>
</html>