(对拼写错误的单词表示歉意) 大家好,大家好 希望你玩得开心. 我是第一次使用AJAX,并试图通过AJAX将一些元素(发送和接收的消息)添加到html中,但我唯一得到的是一个未定义的html文本.

messageInsert和第一次访问正常,但从get_msg我得到一个sql语法错误:

致命错误:未捕获MySQLSQL_EXCEPTION:您的SQL语法有错误;请查看与您的MariaDB服务器版本对应的手册,了解正确的语法,以便在C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php:9堆栈跟踪的第1行使用NEAR‘AND INPING_ID=)OR(OUTENT_ID=AND INFING_ID=)ORDER BY MSG_ID DESC’:#0 C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php(9):MYSQLI->查询(‘SELECT*FROM m...’)#1{Main}在第9行的C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php中抛出

MessageInsert.php:

<?php
    session_start();
    require_once 'connection.php';
    if (isset($_SESSION['userID'])) {
        $outgoing_id = mysqli_real_escape_string(connection() , $_POST['outgoing_id']);
        $incoming_id = mysqli_real_escape_string(connection() , $_POST['incoming_id']);
        $message = mysqli_real_escape_string(connection() , $_POST['message']);
        if (!empty($message)) {
            $query = "INSERT INTO messages (outgoing_id, incoming_id, msg) VALUES ($outgoing_id , $incoming_id , '$message')";
            $result =connection()->query($query);
        }
    }

Get_msg.php:

<?php
    session_start();
    require_once 'connection.php';
    if (isset($_SESSION['userID'])) {
        $outgoing_id = mysqli_real_escape_string(connection() , $_POST['outgoing_id']);
        $incoming_id = mysqli_real_escape_string(connection() , $_POST['incoming_id']);
        $output = "";
        $query = "SELECT * FROM messages WHERE (outgoing_id = {$outgoing_id} AND incoming_id = {$incoming_id}) OR (outgoing_id = {$incoming_id} AND incoming_id = {$outgoing_id}) ORDER BY msg_id DESC";
        $result = connection()->query($query);
        while ($row = mysqli_fetch_assoc($result)) {
            if ($outgoing_id === $row['outgoing_id']) {
                $output ='<div class="message-send my-4">
                               <p>'.$row['msg'].'</p>
                           </div>';
            } else {
                $output = '<div class="message-receive">
                                <img class="message-receive-pic mb-3 me-2" src="style/ProfilePic/'.$_SESSION['userPic'].'" alt="">
                                <div class="message-receive-text-box my-4">
                                    <p> '. $row['msg']  .'</p>
                                </div>
                           </div>';
            }
            echo $output;
        }
    }

我的名字:

let form = document.querySelector(".mySend"),
    sendInput = form.querySelector(".message"),
    sendBtn = form.querySelector(".sendBtn"),
    chatBox = document.querySelector(".chat-box");
form.onsubmit = (e)=> {
    e.preventDefault();
}
sendBtn.onclick = ()=> {
    let xhr = new XMLHttpRequest();
    xhr.open("POST" , "http://localhost/project/myProjects/Chatbot/php/messageInsert.php" , true);
    xhr.onreadystatechange = ()=> {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200){
                sendInput.value = "";
            }
        }
    }
    let formData = new FormData(form);
    xhr.send(formData);
}
setInterval(()=> {
    let xhr = new XMLHttpRequest();
    let data = "";
    xhr.open("POST" , "php/get_msg.php" , true);
    xhr.onload = ()=> {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                data = this.response;
                chatBox.innerHTML = data;
            }
        }
    }
    let formData = new FormData(form);
    xhr.send(formData);
}, 500);

我的html:

<div class="chat-box">

    </div>
    <div class="type-box">
        <form class="mySend" method="post" autocomplete="off">
            <input type="hidden" value="<?php echo $userProfile['id'] ?>" name="incoming_id">
            <input type="hidden" value="<?php echo $_SESSION["profileID"] ?>" name="outgoing_id">
            <input class="message form-control" type="text" placeholder="Type your message here..." name="message">
            <button type="submit" class=" sendBtn btn btn-dark" name="sendBtn"><i class="fab fa-telegram-plane"></i></button>
        </form>
    </div>

我对表名和列名很确定.

非常感谢您的帮助:)

我已经try 了AJAX和php文件中的所有方法,都没有奏效.

推荐答案

您的错误消息为

致命错误:未捕获MySQLSQL_EXCEPTION:您的SQL语法有错误;请查看与您的MariaDB服务器版本对应的手册,了解正确的语法,以便在C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php:9堆栈跟踪的第1行使用NEAR‘AND INPING_ID=)OR(OUTENT_ID=AND INFING_ID=)ORDER BY MSG_ID DESC’:#0 C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php(9):MYSQLI->;查询(‘SELECT*FROM m...’)#1{Main}在第9行的C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php中抛出

清楚地指出了这一行是你遇到麻烦的地方(错误会在你试图执行它的下一行抛出):

$query = "SELECT * FROM messages WHERE (outgoing_id = {$outgoing_id} AND incoming_id = {$incoming_id}) OR (outgoing_id = {$incoming_id} AND incoming_id = {$outgoing_id}) ORDER BY msg_id DESC";

仔细查看错误消息的查询部分:

AND incoming_id = ) OR (outgoing_id = AND incoming_id = ) ORDER BY msg_id DESC

您没有为incoming_idoutgoing_id设置值

以下是您try 加载这些值的方法:

        $outgoing_id = mysqli_real_escape_string(connection() , $_POST['outgoing_id']);
        $incoming_id = mysqli_real_escape_string(connection() , $_POST['incoming_id']);

这清楚地表明您将获得空字符串作为它们的值(因为$_POST['outgoing_id']$_POST['incoming_id']不抛出错误,所以您将这些键放在POST请求中,尽管它们的值为空).

这些值在您的HTML中定义,即在

            <input type="hidden" value="<?php echo $userProfile['id'] ?>" name="incoming_id">
            <input type="hidden" value="<?php echo $_SESSION["profileID"] ?>" name="outgoing_id">

考虑到它们最终是空的,我们知道$userProfile['id']$_SESSION["profileID"]是空的.您将需要研究这些元素是如何定义的,并可能将其包装为intval,如

$intOutgoing_id = intval($outgoing_id);
$intIncoming_id = intval($incoming_id);
$query = "SELECT * FROM messages WHERE (outgoing_id = {$intOutgoing_id} AND incoming_id = {$intIncoming_id}) OR (outgoing_id = {$intIncoming_id} AND incoming_id = {$intOutgoing_id}) ORDER BY msg_id DESC";

因此,即使它们是空字符串,它们也将被转换为0,因此行为将是,如果为它们传递了无效值,则结果集中将有0条记录,而不是页面崩溃.

编辑

构建了这个示例:

    <div class="type-box">
        <form class="mySend" method="post" autocomplete="off">
            <input type="hidden" value="3" name="incoming_id">
            <input type="hidden" value="2" name="outgoing_id">
            <input class="message form-control" type="text" placeholder="Type your message here..." name="message">
            <button type="submit" class=" sendBtn btn btn-dark" name="sendBtn"><i class="fab fa-telegram-plane"></i></button>
        </form>
    </div>
<script>
let form = document.querySelector(".mySend"),
    sendInput = form.querySelector(".message"),
    sendBtn = form.querySelector(".sendBtn"),
    chatBox = document.querySelector(".chat-box");
let xhr = new XMLHttpRequest();
let data = "";
xhr.open("POST" , "bar.html" , true);
xhr.onload = ()=> {
    if (xhr.readyState === XMLHttpRequest.DONE) {
        if (xhr.status === 200) {
            data = xhr.responseText;
        }
    }
}
let formData = new FormData(form);
xhr.send(formData);
</script>

并且它正确地传递了有问题的参数.由于某些因素阻止您的脚本这样做,我建议调试并记录您的代码以查看确切的问题.在这一点上,似乎您的AJAX请求应该可以工作,它在我的测试中成功了.

Php相关问答推荐

PHP如何将ARRAY_MAP与Reset()配合使用

Nginx重写扩展名为.php的动态URL

Laravel 10 -扩展现有的artisan命令?

我必须对所有的SQL查询使用预准备语句吗?

如何使用MS Graph PHP SDK V2下载文件

如何从Laravel中的Spatie\Dns\Records\A对象中提取IP地址

HTAccess重写一些URL,但将所有其他URL发送到另一个页面

从字符串转换日期和/或时间时,Laravel转换失败

函数存储到变量中

如何在自定义邮箱内容中获取WooCommerce订单项目的详细信息?

如何批量处理消息总线中的消息

在具有Angular 的 Laravel 应用程序中,不显示图像

图片上传成功

添加产品标签以通过 WooCommerce 邮箱通知订购商品

htaccess Rewriterule 无法以任何方式工作

如何通过帖子自定义元数据进行查询

Shopware 6.4.20店铺激活问题

Laravel 从嵌套数组的第二级获取最后一项

如何在运行 update_post_meta 后更新 wp_wc_product_attributes_lookup 表?

用于多态服务的 Symfony setter 注入