JavaScript更新MySQL? [英] JavaScript to update MySQL?

查看:78
本文介绍了JavaScript更新MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:)

我希望建立一个非常简单的评级系统。它不会包含任何像平均值,它实际上是投票或投票,所以如果有更多的投票,它将进入一个负面的立场。

I'm hoping to make a very simple rating system. It won't consist of anything like averages, it's literally vote up or vote down, so if there's more votes down it'll go into a minus stance.

我是什么我想点击点击投票/关闭的链接时,页面不会刷新,只有那个评级号码。一旦调用新数据,我猜测我可以用JavaScript附加,但是我不知道如何用JavaScript运行MySQL查询。

What I'd like is for when the links to vote up/down are clicked, the page isn't refreshed, just that rating number. I'm guessing I can do this with JavaScript's append once it calls the new data, however I've no idea how to run the MySQL query with JavaScript.

从什么我明白,这不是那么安全,所以我希望我能从PHP文件中运行它吗?

From what I understand, this isn't all that safe so I'm hoping I can run it from a PHP file?

有人能告诉我怎么做吗?

Can anyone tell me how to do this please?

推荐答案

您必须在PHP文件中进行SQL更新查询,并通过AJAX执行该PHP脚本。例如:

You have to have the SQL update query in a PHP file and execute that PHP script via AJAX. For example:

在PHP中:

$page_id = mysql_real_escape_string(html_entities($_POST['page_id']));
$rating = mysql_real_escape_string(html_entities($_POST['rating']));

mysql_query(" UPDATE ratings(vote) VALUES ('$rating') WHERE id = '$page_id' ");

AJAX(假设您使用的是jQuery):

AJAX (assuming you are using jQuery):

function rate(rating, page_id)
{

   $.ajax({
      url: 'path/to/php_script.php',
      type: 'post',
      data: 'rating='+rating+'&page_id='+page_id,
      success: function(output) 
      {
          alert('success, server says '+output);
      }, error: function()
      {
          alert('something went wrong, rating failed');
      }
   });

}

HTML:

<form>   
   Like: <input type="button" value="Like" onClick="rate(1, $_GET['page_id'])" />
   <br />
   Hate: <input type="button" value="Hate" onClick="rate(2, $_GET['page_id'])" />
</form>

这篇关于JavaScript更新MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆