【问题标题】:Delete all rows in a table with MySQL?使用 MySQL 删除表中的所有行?
【发布时间】:2014-04-07 14:46:52
【问题描述】:

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

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>

【问题讨论】:

  • 如果 $_POST 为空,那么您应该显示您的客户端 html/表单代码。
  • 该信息已添加!

标签: php mysql ajax json


【解决方案1】:

主要问题:

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

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

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

【讨论】:

  • 我已将其更新为 ajax,但现在我的消息没有显示,并且重置按钮仍然无法清除旧消息。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2012-11-19
      • 2023-04-09
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多