单击按钮就触发SQL查询? [英] Firing SQL query on click of button?
本文介绍了单击按钮就触发SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的PHP/MySQL计数器,它可以递增地增加SQL数据库中的值.当前,此脚本在每次页面刷新时都会触发,这意味着该数字在每次刷新页面时都会增加1.我想更改它,使其仅在按下按钮时才增加.
I have a simple PHP/MySQL counter which incrementally increases a value in my SQL database. Currently, this script fires every time the page refreshes meaning that the number increases by 1 every time the page is refreshed. I would like to change it so that it only increases when a button is pressed.
这是我的PHP代码:
<?php
error_reporting(0);
include("config.php");
// get click details based on ID
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id='1'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$sql = "UPDATE ".$SETTINGS["data_table"]." SET clicks=clicks+1 WHERE id='1'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
?>
这是我想将其绑定到的按钮:
And here is the button I would like to tie it to:
<div class="content">
<span id="button">Click Me</span>
</div>
关于如何实现此目标的任何想法?谢谢!
Any ideas on how I can accomplish this? Thanks!
推荐答案
选项1(轻松):
点击按钮重新加载页面:
Reload the page on clicking the button:
<span id="button" onclick="javascript: location.reload();">Click Me</span>
选项2:
- 将查询部分放在单独的文件中(例如 inc.php )
- 在按钮范围的onclick事件上使用AJAX调用. (您可以使用JQuery)
以下是jQuery AJAX的示例:
Here is an example with jQuery AJAX:
<script type="text/javascript">
<!--
function inc_counter()
{
$.ajax({
url: "inc.php",
context: document.body
}).done(function() {
alert('incremented');
});
return false;
}
-->
</script>
<span id="button" onclick="javascript:return inc_counter();">Click Me</span>
这篇关于单击按钮就触发SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文