简体   繁体   English

使用 AJAX 将数据从 Javascript 发送到 PHP 以更新现有的 Z9EDB3C572B56B91542AF65948051 数据库

[英]Send data from Javascript to PHP using AJAX for updating the existing Mysql database

I am working on the backend of my snake game project.I have been trying to update Max entry of mysql database by sending the new highscore to update.php webpage using xmlhttp request through post request of ajax.我正在研究我的蛇游戏项目的后端。我一直在尝试通过将新的高分发送到 update.php 网页来更新 mysql 数据库的最大条目。

In order to test whether my data is sent to update.php,I have also sent the test=all good to update.php.But when I am echoing,it is not displayed in the web browser thereby showing that no such data was successfully sent.为了测试我的数据是否发送到update.php,我也发送了test=all good to update.php。但是当我回显时,它没有显示在Z2567A5EC9705EB7AC2C984033E06浏览器中成功显示没有这样的数据发送。

Please tell me why is it happening?请告诉我为什么会这样?

This is my code section of xml http request.这是我的 xml http 请求的代码部分。

    '''
    highscore=score;
    document.querySelector("#my-high-score").textContent=highscore;
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "update.php", true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form- 
    urlencoded");
    xhttp.send(`highest_score=${highscore}&test="all good"`);
    '''

And this is the update.php file这是 update.php 文件

    '''
    <?php
    session_start();
    $link=mysqli_connect("shareddb-x.hosting.stackcp.net","gameusers- 
    31353746432","havd5n897","gameusers- 
    3135374592");
    if(mysqli_connect_error())
    die ("connection is not successfull");
    echo "welcome user";
    $x=90;
    echo $x;
    echo $_POST['test'];
    if($_POST['highest_score'])
    {
    $high_score=$_POST['highest_score'];
    $email=$_SESSION['email'];
    if($_SESSION['highscore']<$high_score)
    {
    $query="UPDATE gameusers SET Max=$high_score WHERE Email= $email LIMIT 
    1";
    echo $high_score;
    mysqli_query($link,$query);
    echo "all fit";
    }
    }
    ?>
    '''

Please someone tell me where I am actually commiting the mistakes.请有人告诉我我实际上在哪里犯了错误。

Try to remove to double quotes in "all good"尝试删除“都很好”中的双引号

xhttp.send(`highest_score=${highscore}&test="all good"`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM