简体   繁体   English

我需要3秒的间隔来执行每个mysql_query

[英]I need an interval of 3 seconds to execute each mysql_query

I'm doing a simple chat and everything is working well, but everybody can flood the chat sending how many messages they want to... 我正在做一个简单的聊天,并且一切正常,但是每个人都可以充斥整个聊天,发送他们想发送的消息数量...

PHP: PHP:

<div id="chat_panel"></div>
<br />
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="text" autocomplete="off" maxlength="50" placeholder="Type your message..." id="message" size="87%" checked="yes"></td>
<td><input type="button" id="message" onClick="sendMessage()" value="Send"></td>
</tr>
</table>

JAVASCRIPT: JAVASCRIPT:

function sendMessage() {
var message = $('#message').val();
$.post('postMessage.php', { message: message } , function() { } );
}

postMessage.PHP: postMessage.PHP:

session_start();
include ("chats/connect.php");
$timname = $_SESSION['username'];
if (isset($_POST['message'])) {
mysql_query("INSERT INTO chat VALUES ('', '".$timname."', '".$_POST['message']."', '".time()."', NOW(), '');");
}

I need to set a interval to send each message like 3 seconds... Any information will be helpful! 我需要设置一个间隔来发送每条消息,例如3秒...任何信息都会有所帮助!

Try Jquery to refresh specific page. 尝试使用Jquery刷新特定页面。 Try something like below code 试试下面的代码

setup.js setup.js

var autorefresh=setInterval(
function()
{
    $("query.php").load('query.php');   
    e.preventDefault();
},3000);

query.php query.php

session_start();
include ("chats/connect.php");
$timname = $_SESSION['username'];
if (isset($_POST['message'])) {
mysql_query("INSERT INTO chat VALUES ('', '".$timname."', '".$_POST['message']."', '".time()."', NOW(), '');");
}

Here 3000 in setup.js is time in miliseconds you want to refresh. 在setup.js中,这里的3000是您要刷新的时间(以毫秒为单位)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM