用MySQL删除表中的所有行?

时间:2014-04-07 14:46:53

标签: php mysql ajax json

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

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>

3 个答案:

答案 0 :(得分:1)

主要问题:

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

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

.getJSON()仅适用于 GET 请求。如果你想使用POST,你将不得不使用$ .ajax()。

答案 1 :(得分:1)

您正在使用ajax执行GET请求。发出POST请求。添加类型。例如

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

答案 2 :(得分:0)

我建议你能做的最好的事情是:使用PHP REQUEST变量。使用它可以接受post或get请求。即例如: if ( isset($_REQUEST['reset']) ) { /***Code to delete chat **/ }