gpt4 book ai didi

PHP自动回复消息在添加回复到MySQL数据库之前需要暂停

转载 作者:行者123 更新时间:2023-11-29 21:44:25 24 4
gpt4 key购买 nike

我在这里使用一个非常非常简单的方法,使用基本的 PHP 来开始使用我已经工作的消息功能的自动回复系统。我知道我在这里没有使用最好的安全方法,但这不是本文的目的,所以请避免评论脚本的安全性。

现在,当用户从名为 reply 的表单发送回复时,它会决定用户是否正在与自动回复功能对话,user_id 0 。确实如此,$sarssystem 返回为 1。如果它不返回为 1 表单将作为一般消息进行处理,效果完美。以下是表单流程:

///////////// ADD REPLY TO CONVERSATION //////////////////////////////
if(isset($_POST['reply'])){
$user_id = $_SESSION['userid'];
$message = $_POST['message'];
$conversation_id = $_POST['conversation_id'];
$sarssystem = $_POST['sarssystem'];

if(isset($sarssystem)){
if($sarssystem == 1){
include 'system/sars_system.php';
} else {

$reply = str_replace("'","\\'",$message);


mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)
VALUES ('','$reply','$user_id', NOW(), '', '$conversation_id')");

mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");

}
} else {

$reply = str_replace("'","\\'",$message);


mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)
VALUES ('','$reply','$user_id', NOW(), '', '$conversation_id')");

mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");

}
}
//////////////////////////////////////////////////////////////////////

如果它返回为 1 并且您正在回复自动服务,它将包含一个文件以将您的消息和自动回复添加到数据库中:

系统/sars_system.php:

if($message == 'hello'){
$sarsreply = 'hey, how are you?';
}

$usr_message = str_replace("'","\\'",$message);

mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)
VALUES ('','$usr_message','$user_id', NOW(), NOW(), '$conversation_id')");

mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");

sleep(3);

mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)
VALUES ('','$sarsreply','0', NOW(), '', '$conversation_id')");

正如你所看到的,它是非常基本的,并且只是一个让它工作的测试,一旦它正常工作,我就可以做更多的工作,我遇到的问题是尝试添加用户回复并显示它通常,然后等待几秒钟,然后再添加自动回复。我尝试使用 sleep() 函数,如您所见,但这只是在我点击发送添加回复时延迟了整个页面,整个页面似乎卡住了 3 秒,然后用户和自动回复同时在屏幕上飞翔。我尝试先添加用户回复,然后等待几秒钟,然后将自动回复添加到数据库中。除了 sleep() 之外,是否还有其他函数可以用来获取这些结果?

按要求 - 检索消息和显示对话的代码:

$conversation_id = $convoid;   
$res4=mysqli_query($conn, "SELECT * FROM ap_conversations WHERE conversation_id = '$conversation_id'");
while($row4=mysqli_fetch_array($res4))
{
$co_conversation_id = $row4['conversation_id'];
$co_user_one = $row4['user_one'];
$co_user_two = $row4['user_two'];
if($co_user_one == $user_id){
$co_recip = $co_user_two;
} else if($co_user_two == $user_id){
$co_recip = $co_user_one;
}
if($co_recip == '0'){
$sarssystem = 1;
} else {
$sarssystem = 0;
}
$res5=mysqli_query($conn, "SELECT * FROM ap_messages WHERE conversation_id = '$conversation_id'");
while($row5=mysqli_fetch_array($res5))
{
$co_message_id = $row5['message_id'];
$co_message = $row5['message'];
$co_sender_id = $row5['sender_id'];
$co_time_read = $row5['time_read'];

}
$res6=mysqli_query($conn, "SELECT * FROM ap_users WHERE user_id = '$co_recip'");
while($row6=mysqli_fetch_array($res6))
{
$co_first_name = $row6['first_name'];
$co_last_name = $row6['last_name'];
}



?>
<div class="col-xs-12 col-md-8">
<div class="panel panel-default">
<div class="panel-heading">
<div style="display:inline"><? echo ''.$co_first_name.' '.$co_last_name.''; ?></div> <div align="right" style="display:inline; float:right"><button type="button" class="btn btn-primary btn-sm" onclick="location.href='messages.php';"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> New</button></div>
</div>
<div class="panel-body">
<?
}
?>
<div class="list-group-message" style="overflow-y: scroll;height:385px;width:680px">
<?
$res6=mysqli_query($conn, "SELECT * FROM ap_messages WHERE conversation_id = '$conversation_id' ORDER BY time_sent ASC");
while($row6=mysqli_fetch_array($res6))
{
$me_message = $row6['message'];
$me_message_id = $row6['message_id'];
$me_sender_id = $row6['sender_id'];
$todaysdate = date('d/m/Y');
$me_time_sent_date = date('d/m/Y', strtotime($row6['time_sent']));
$me_time_sent_date_and_time = date('d/m/Y H:i:s', strtotime($row6['time_sent']));
$me_time_sent_time = date('H:i', strtotime($row6['time_sent']));
if($todaysdate == $me_time_sent_date){
$me_time = ''.$me_time_sent_time.'';
} else {
$me_time = ''.$me_time_sent_date.' '.$me_time_sent_time.'';
}


$me_time_read = $row6['time_read'];
$res7=mysqli_query($conn, "SELECT * FROM ap_users WHERE user_id = '$me_sender_id'");
while($row7=mysqli_fetch_array($res7))
{
$me_first_name = $row7['first_name'];
$me_last_name = $row7['last_name'];
$me_display_img = $row7['display_img'];
}

mysqli_query($conn, "UPDATE ap_messages SET time_read = NOW() WHERE message_id = '{$me_message_id}' AND time_read = '0000-00-00 00:00:00' AND conversation_id = '$co_conversation_id' AND sender_id != '$user_id'");
?>




<div class="media" style="max-width: <? echo $screenwidth; ?>px;">
<div class="media-left">
<a href="#">
<img src="userimg/<? echo $me_display_img; ?>" alt="user" width="64px" height="64px" hspace="10px" class="media-object" align="left">
</a>
</div>
<div class="media-body" style="position: relative !important;">
<div style="display:inline"><b><a href=""><? echo ''.$me_first_name.' '.$me_last_name.''; ?></a></b></div> <div align="right" style="float:right; display:inline"> <? echo $me_time; ?> </div><br>
<? echo $me_message; ?>
</div>
</div>

<?
}
?>

最佳答案

根据评论中的建议,我将使用 DATE_ADD() 增加时间戳:

if($message == 'hello'){
$sarsreply = 'hey, how are you?';
}

$usr_message = str_replace("'","\\'",$message);

mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)
VALUES ('','$usr_message','$user_id', NOW(), NOW(), '$conversation_id')");

mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");

mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)
VALUES ('','$sarsreply','0', DATE_ADD( NOW(), INTERVAL 5 SECOND), '', '$conversation_id')");

关于PHP自动回复消息在添加回复到MySQL数据库之前需要暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34214848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com