我需要一個簡單代碼的幫助,該代碼將在單擊我的網站上的鏈接時更新MySQL中的字段(+1)。這是我的評論數據庫上的「報告」按鈕。PHP/MySQL>單擊鏈接時更新字段
所以我可以看到有多少次評論已被「報道」。腳本不重定向或加載新頁面很重要,只是迴應消息或JS警報。
感謝您的任何幫助。
我需要一個簡單代碼的幫助,該代碼將在單擊我的網站上的鏈接時更新MySQL中的字段(+1)。這是我的評論數據庫上的「報告」按鈕。PHP/MySQL>單擊鏈接時更新字段
所以我可以看到有多少次評論已被「報道」。腳本不重定向或加載新頁面很重要,只是迴應消息或JS警報。
感謝您的任何幫助。
$sql = "update `table` set `increment` = `increment` + 1 where `link` = '".$link."'";
這只是查詢。你應該看看jquery ajax來處理請求
在some.php你需要一種方式來閱讀帖子。
<?php
if(isset($_POST['clicked_row']))
{
$sql = "UPDATE $row_to_update SET increment = increment + 1 WHERE id = '".$_POST['clicked_row']."'";
mysql_query($sql);
}
?>
假設你在你的Ajax請求,這是在jQuery的崗位的數據參數發送行ID,你的HTML將需要像<a href="javascript:increment(row_id);">Add click</a>
所以jQuery的調用知道要送些什麼行。 php
這是一個非常棒的方式來防止參與本網站。點擊減號箭頭是爲了什麼原因?他說簡單。根據他的問題,看起來他似乎不知道你可以通過聲明'row = row + 1'來增加數據庫表上的計數,所以答案是有效的。如果不是,什麼?整個jquery語句加php和mysql的post腳本?沒有提供表名,所以你必須得到通用的。那麼爲什麼在這個問題上不是每個答案都對接?嚴重的是,人。 – 2011-02-03 18:23:56
感謝您的回答。我可以執行查詢,但如何在點擊鏈接時運行它? – Tom 2011-02-03 18:27:45
這真的不簡單。
你應該看看這裏: http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php
您的SQL會是這個樣子。
"UPDATE post SET flagcount = flagcount + 1 WHERE postID = {$myPostID}"
使用jQuery發送AJAX請求:
$.post('yourScript.php', {commentID: yourCommentId});
第二個參數是該請求的數據。改變這個以適應你的需求。如果你想執行一個JavaScript函數後的請求已完成:
$.post('yourScript.php', {commentID: yourCommentId}, function(data) {
});
是的,你應該找AJAX作出POST請求,其中一些升級數據庫中的回答你的PHP腳本。在jQuery中是這樣的嗎?
$('a').click(function(event) {
$.ajax({
type: "POST",
url: "some.php", // page where insertion to database should have made
data: "name=John",
success: function(msg){
alert("Counter updated");
}
});
});
我可以使用表單嗎?
<form method="post" action="report.php">
<input type="hidden" name="report">
<input type="submit" name="submit" value="report">
</form>
然後在report.php文件中查詢?
你可以做什麼的大綱。我做了一些假設,並使用URL方法而不是POST來選擇GET;編輯品嚐,如果你喜歡使用POST。
另一個說明 - 你可能想要一個標誌表,以便讓你跟隨誰標記誰,也許讓他們添加評論等......只是一個想法。
這只是給你一個想法,你怎麼可以做你所要求的。我沒有測試代碼,所以買家要小心。這只是一個起點。
view.php
這是你的最終用戶會點擊激活標誌腳本的鏈接。在你的標記的某個地方,你會有類似...
<a class="flag" href="flag.php?comment=100">Flag</a>
然後,view.php內容中,你將有點擊事件代碼,在這種情況下,嘗試一個jQuery異步JavaScript請求($阿賈克斯(),可能是$不用彷徨或$。員額,或另一個類似MooTools或Prototype的庫)。
該示例演示了服務器將返回數據參數中的JSON格式的文本,然後評估該服務器以檢查服務器的響應情況(成功?失敗?部分成功?需要登錄?)。請參閱http://json.org/。
<script type="text/javascript">
$(document).ready(function(){
$('a.flag').click(function(event){
$.ajax({
type: "GET",
url: $(this).attr('href'),
data: dataString,
dataType: "json",
success: function (data) {
if (data.error == -1) {
// Not logged in, redirect to login page.
window.location = 'login.php';
} else if (data.flagged == 1 && data.count != -1) {
// Success! With a count too.
alert('Comment flagged ('+data.count+' times flagged).');
} else {
switch(data.error) {
case 1:
alert('Comment not found');
break;
case 2:
alert('Comment not flagged due to an update error.');
break;
case 3:
alert('Comment flagged but count not returned.');
break;
default:
alert('There was a general error flagging the comment.');
break;
}
}
},
error: function(){
alert('Comment not flagged; general send error.');
}
});
// Call these to prevent the a tag from redirecting
// the browser to the a-tags href url.
event.preventDefault();
return false;
});
});
</script>
flag.php
flag.php是服務器頁面,允許你運行你的MySQL查詢,當然使用PHP,然後用一些JSON格式的文本響應。通過返回的頁面內容(文本)看起來是這樣的:
{"flagged":1,"count":15,"error":0}
這表明你的調用頁面(瀏覽器),該評論被舉報,已被標記15次(壞,壞的評論) ,而且沒有錯誤。
這很重要:這是而不是相當於HTML。這種類型的文本是數據,並在響應$ .ajax()函數時被解析回Javascript對象。所以,不要在它周圍放置任何HTML,因爲這不是你應該做的。示例請參閱http://www.json.org/example.html。
<?php
// Need to output JSON headers and try to prevent caching.
session_cache_limiter('nocache');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
// Our JSON array to return
// - flagged would be 1 = success, 0 = failure
// - count would return the # flags, with -1 no return #
// - error code, see comments for description
$json = array('flagged'=>0,'count'=>-1,'error'=>0);
// Your logged in check code goes here
// you don't want non-logged in people doing this
// Here, however you test to find out if someone is logged in,
// check and return a -1 error to redirect the login.
if (!$logged_in) {
// error -1 = not logged in, redirect browser
$json['error'] = -1;
exit(echo(json_encode($json)));
}
// Your mysql connection code goes here
$comment = mysql_real_escape_string($_GET['comment']);
if (empty($comment) || !is_numeric($comment)) {
// error 1 = comment id not found
$json['error'] = 1;
} else {
$result = mysql_query("
UPDATE comments
SET flags = flags+1
WHERE commentID = $comment
");
if (!$result) {
$json['flagged'] = 0;
// error 2 = update error
$json['error'] = 2;
} else {
$json['flagged'] = 1;
$count = mysql_query("
SELECT flags
FROM comments
WHERE commentID = $comment
LIMIT 0, 1
");
if ($count) {
$query = mysql_fetch_assoc($count);
$json['count'] = $query['count'];
} else {
// error 3 = updated but did not get count
$json['error'] = 3;
}
}
}
echo json_encode($json);
?>
我認爲您正在尋找AJAX。 – Konerak 2011-02-03 18:15:04
+1歡迎來到SO。你更好地解釋你的問題,你會得到準確的答案:)你也可以再次編輯你的問題,並與那些回答下面的評論的人交流:) – Sarfraz 2011-02-03 18:18:04
你試過了什麼嗎?你在尋找一個你可以做什麼的大綱,實際的代碼(這裏有人爲你做),或者是什麼? – 2011-02-03 18:34:33