用MySQL删除表中的所有行?

时间:2022-12-25 09:18:07

I'm trying to create a simple chat application that posts people's messages, and that gives the user an option to "reset" the chat which will delete all messages from the database so the user can start over. The messages post okay, but the reset button just sends an empty post (instead of deleting all current posts). I'm wondering what I'm doing wrong:

我正在尝试创建一个简单的聊天应用程序来发布人们的消息,这让用户可以选择“重置”聊天,这样就可以从数据库中删除所有的消息,这样用户就可以重新开始了。消息post ok,但是reset按钮只发送一个空的post(而不是删除所有当前的post)。我在想我做错了什么:

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;
}

Per a comment below, I've updated my client side code to be:

下面的评论,我已经更新了我的客户端代码:

<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>

The code in its entirety, in case I've missed something/context is helpful:

完整的代码,以防我遗漏了一些东西/上下文是有用的:

<?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>

3 个解决方案

#1


1  

Major issue:

主要问题:

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

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

.getJSON() is for GET requests only. If you want to use a POST, you'll have to use $.ajax() instead.

. getjson()只用于获取请求。如果您想使用POST,则必须使用$.ajax()。

#2


1  

You are doing a GET request using ajax. Make a POST request. Add Type. For Example

您正在使用ajax进行GET请求。做一个POST请求。添加类型。例如

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

#3


0  

I suggests best thing you can do is: Use PHP REQUEST variable. Using it use can accept either post or get requests. i.e. For example: if ( isset($_REQUEST['reset']) ) { /***Code to delete chat **/ }

我建议您可以做的最好的事情是:使用PHP请求变量。使用它可以接受post或get请求。例如:if (isset($_REQUEST['reset'])){/***代码删除聊天**/}

#1


1  

Major issue:

主要问题:

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

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

.getJSON() is for GET requests only. If you want to use a POST, you'll have to use $.ajax() instead.

. getjson()只用于获取请求。如果您想使用POST,则必须使用$.ajax()。

#2


1  

You are doing a GET request using ajax. Make a POST request. Add Type. For Example

您正在使用ajax进行GET请求。做一个POST请求。添加类型。例如

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

#3


0  

I suggests best thing you can do is: Use PHP REQUEST variable. Using it use can accept either post or get requests. i.e. For example: if ( isset($_REQUEST['reset']) ) { /***Code to delete chat **/ }

我建议您可以做的最好的事情是:使用PHP请求变量。使用它可以接受post或get请求。例如:if (isset($_REQUEST['reset'])){/***代码删除聊天**/}