Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP&;AJAX将两次冲突插入查询?_Php_Jquery_Ajax_Pdo - Fatal编程技术网

PHP&;AJAX将两次冲突插入查询?

PHP&;AJAX将两次冲突插入查询?,php,jquery,ajax,pdo,Php,Jquery,Ajax,Pdo,因此,我正在用PHP/PDO/AJAX制作一个基本的聊天框,最近,当我添加AJAX代码并提交我的shout时,它运行了两次查询,我已经检查了代码,检查者说他们看不到任何错误。所以我想知道你是否可以帮我找到错误 shout.php <html> <head> <title>Shout!</title> <link rel="stylesheet" href="CSS/styles.css"/> </head>

因此,我正在用PHP/PDO/AJAX制作一个基本的聊天框,最近,当我添加AJAX代码并提交我的shout时,它运行了两次查询,我已经检查了代码,检查者说他们看不到任何错误。所以我想知道你是否可以帮我找到错误

shout.php

<html>
<head>
    <title>Shout!</title>
    <link  rel="stylesheet" href="CSS/styles.css"/>
</head>  
</html>

<?php

include 'auth.login.php';
include 'pdo.config.php';

if (!isset($_SESSION['Username'])) {
echo '<br/>';
echo '<center>You need to login to post!<br/>';
header("Refresh:2; URL=index.php");
exit();
}

$Username = $_SESSION['Username'];

if (!isset($_POST['Message']) || empty($_POST['Message'])) {
echo '<br/>';
echo '<center>How do you expect to send a message if the field is empty, ey?<br/>';
header("Refresh:2; URL=index.php");
exit();
} else {

$Message = htmlspecialchars($_POST['Message']);

$insertMessage = $PDO->prepare("INSERT INTO `chatbox` (User, Message) VALUES (?, ?)");
$insertMessage->bindParam(1, $Username, PDO::PARAM_STR);
$insertMessage->bindParam(2, $Message, PDO::PARAM_STR);

if ($insertMessage->execute()) {
    header("Location: index.php");
    exit();
} else {
    echo '<br/>';
    echo '<center>Aw snap, shout could not be added!';
    header("Refresh:2; URL=index.php");
    exit();
}
}

?>

呼喊
main.js

$(document).ready(function(){

$.ajax({

    url: 'data.php',
    data: '',
    dataType: 'json',
    type: 'GET',
    success: function(data)
    { 
$.each(data, function (idx, elem) {
$('#messageDisplay').append("<div>["+elem.SentOn+"] "+elem.User+": "
+ elem.Message + "</div>");
        });
    }
});
});
$(文档).ready(函数(){
$.ajax({
url:'data.php',
数据:“”,
数据类型:“json”,
键入:“GET”,
成功:功能(数据)
{ 
$.each(数据、函数(idx、元素){
$('#messageDisplay').append(“[”+elem.SentOn+“]”+elem.User+”:“
+元素消息+“”);
});
}
});
});
我的表格:

<form action="shout.php" method="post">
<textarea rows="8" cols="74" id="Message" name="Message" placeholder="Post messages here."></textarea> 
<br/><input type="submit" id="submit" value="Shout!">
                </form>


data.php

<?php

include 'pdo.config.php';


$chat = $PDO->query("SELECT * FROM `chatbox`");
$getRow = $chat->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($getRow);

?>

下一步是检查access.log,以确保您没有收到来自shout.php的双重帖子


“我的表单:”--index.php中的表单是哪个文件?除了shout.php在关闭html标记后输出html之外,我没有看到任何错误。@developerwjk,是的。为了我的好奇心,你能删除header()之前的输出吗?”在shout.php中发送-我特别关注
execute
condition的else语句[以及页面顶部的块],它提供输出,然后发送头。我并不期望它能解决这个问题,但没有任何其他潜在的问题向我扑来。