gpt4 book ai didi

php - 使用 MySQL 删除表中的所有行?

转载 作者:行者123 更新时间:2023-11-29 08:04:59 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的聊天应用程序来发布人们的消息,并为用户提供“重置”聊天的选项,这将从数据库中删除所有消息,以便用户可以重新开始。消息发布正常,但重置按钮只是发送一个空帖子(而不是删除所有当前帖子)。我想知道我做错了什么:

if ( isset($_POST['reset']) ) {
$sql = "DELETE FROM {$p}sample_chat WHERE chat = :CHA";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':CHA' => $_POST['message']));
header( 'Location: '.sessionize('index.php') ) ;
return;
}

根据下面的评论,我已将客户端代码更新为:

<html>
<script type="text/javascript" src="<?php echo($CFG->staticroot); ?>/static/js/jquery-1.10.2.min.js"></script>

<body>

<form id="chats" method="post">
<input type="text" size="60" name="message" />
<input type="submit" value="Chat"/>
<input type="submit" name="reset" value="Reset"/>
<a style="color:grey" href="chatlist.php" target="_blank">Launch chatlist.php</a>
</form>
<p id="messages" >
<script type="text/javascript">
function htmlentities(str) {
return $('<div/>').text(str).html();
}

function updateMsg() {
window.console && console.log("Requesting JSON");
$.ajax({
url: '<?php echo(sessionize('chatlist.php')); ?>',
cache: false,
success: function(data){
window.console && console.log("JSON Received");
window.console && console.log(data);
$("#chatcontent").empty();
for (var i = 0; i < data.length; i++) {
entry = data[i];
$("#chatcontent").append("<p>"+entry[0] +
"<br/>&nbsp;&nbsp;"+entry[1]+"</p>\n");
window.console && console.log("entry " + entry[0]);
}
setTimeout('updateMsg()', 4000);
}
});
}
window.console && console.log("Startup complete");
updateMsg();

</script>
</p>
</body>

完整的代码,如果我错过了某些内容/上下文,会很有帮助:

<?php
require_once "../../config.php";
require_once $CFG->dirroot."/pdo.php";
require_once $CFG->dirroot."/lib/lms_lib.php";

// This is a very minimal index.php - just enough to launch
// chatlist.php with the PHPSESSIONID parameter
session_start();

// Retrieve the launch data if present
$LTI = requireData(array('user_id', 'result_id', 'role','link_id'));
$instructor = isset($LTI['role']) && $LTI['role'] == 1 ;
$p = $CFG->dbprefix;

if ( isset($_POST['message']) ) {
$sql = "INSERT INTO {$p}sample_chat
(link_id, user_id, chat, created_at)
VALUES (:LI, :UID, :CHA, NOW() )
ON DUPLICATE KEY
UPDATE chat = :CHA, created_at = NOW()";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':LI' => $LTI['link_id'],
':UID' => $LTI['user_id'],
':CHA' => $_POST['message']));
$messages = array();
header( 'Location: '.sessionize('index.php') ) ;
return;
}

if ( isset($_POST['reset']) ) {
$sql = "DELETE FROM {$p}sample_chat WHERE chat = :CHA";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':CHA' => $_POST['message']));
header( 'Location: '.sessionize('index.php') ) ;
return;
}

?>

<html>
<script type="text/javascript" src="<?php echo($CFG->staticroot); ?>/static/js/jquery-1.10.2.min.js"></script>

<body>

<form id="chats" method="post">
<input type="text" size="60" name="message" />
<input type="submit" value="Chat"/>
<input type="submit" name="reset" value="Reset"/>
<a style="color:grey" href="chatlist.php" target="_blank">Launch chatlist.php</a>
</form>
<p id="messages" >
<script type="text/javascript">
function htmlentities(str) {
return $('<div/>').text(str).html();
}

function updateMsg() {
window.console && console.log("Requesting JSON");
$.ajax({
url: '<?php echo(sessionize('chatlist.php')); ?>',
cache: false,
success: function(data){
window.console && console.log("JSON Received");
window.console && console.log(data);
$("#chatcontent").empty();
for (var i = 0; i < data.length; i++) {
entry = data[i];
$("#chatcontent").append("<p>"+entry[0] +
"<br/>&nbsp;&nbsp;"+entry[1]+"</p>\n");
window.console && console.log("entry " + entry[0]);
}
setTimeout('updateMsg()', 4000);
}
});
}
window.console && console.log("Startup complete");
updateMsg();

</script>
</p>
</body>

最佳答案

主要问题:

 $.getJSON('<?php echo(sessionize('chatlist.php')); ?>', function(data){
^^^--- using http GET

if ( isset($_POST['reset']) ) {
^^^^---expecting HTTP POST

.getJSON() 仅适用于 GET 请求。如果您想使用 POST,则必须使用 $.ajax()。

关于php - 使用 MySQL 删除表中的所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22915776/

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