javascript - 使用 POST 从 javascript 发送的数据插入到使用 php 的 mysql 数据库中

标签 javascript php mysql ajax

我一直在尝试将数据插入到 mysql 数据库的表中。该数据是通过 ajax 使用 POST 方法发送的。但是,当我尝试将其插入数据库时​​,没有任何反应。

这是将数据发送到 php 文件的 javascript 函数。

 addToCart: function(itemId,userId){
              let request = new XMLHttpRequest();
              request.open("POST", "../E-CommerceCore/addToCart.php?
              itemId="+ itemId + "?userId=" + userId, true);
              request.send();
            },

这里是使用它的地方。它嵌套在一个更大的函数中,因此这就是 book[i].Id 的来源。

   document.getElementById('add-to-cart').onclick = function(){
    cartFunctions.addToCart(book[i].Id, '1');
   };

因此,这需要一个项目 ID 和一个用户 ID,并将它们存储在此处的 php 变量中。

 class Cart
{
  public function addToCart($item,$user){
    include 'connect.php';
    $query = $bookStore->prepare("INSERT INTO cart SET item_Id=?, user_Id=?");
    $query->execute([$item,$user]);
  }
}

$cartManager = Cart();
$itemId = $_REQUEST["itemId"];
$userId = $_REQUEST["userId"];

$cartManager->addToCart("$itemId","$userId");

然后,该 php 文件运行 addToCart 函数,该函数应将其插入表中。这就是我遇到问题的地方,因为当用户单击按钮时,没有数据插入到数据库中。我将 connect.php 文件用于另一个 Controller ,该 Controller 从同一数据库中的不同表中进行选择(如果这是一个问题),是的,我已经检查以确保与数据库的连接良好。任何见解将不胜感激。请不要使用 jQuery 解决方案。感谢您投入的时间和精力。

最佳答案

request.open("POST", "../E-CommerceCore/addToCart.php? itemId="+ itemId + "?userId="+ userId, true); 您正在发送参数作为带有 url 的 GET ,并且由于您使用另一个 ? 来分隔 2 个参数,因此您又犯了另一个错误。请点击此链接发送您的数据:Send POST data using XMLHttpRequest

var http = new XMLHttpRequest();
var url = "path_to_file.php";
var params = "itemId="+ itemId + "&userId=" + userId; //Please note that the 2 params are separated by an **&** not a **?** as in your question
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
    alert(http.responseText);
    }
}
http.send(params);

传递参数时,这里的引号是不必要的:

$cartManager->addToCart("$itemId","$userId");

如果可能,请在调用 addToCart 方法之前尝试 var_dump($_REQUEST),以确保参数已通过 javascript 请求成功发送。

现在,关于 sql 查询,您必须更新类并使用 bindParam,然后调用 execute。我已更新您的 php 代码如下:

class Cart{
  public function addToCart($item,$user){
        include 'connect.php';
        $query = $bookStore->prepare("INSERT INTO cart SET item_Id=:item_id, user_Id=:user_id");
        $query->bindParam(':item_id', $item);
        $query->bindParam(':user_id', $user);
        $query->execute();
    }
}

$cartManager = new Cart();
$itemId = $_REQUEST["itemId"];
$userId = $_REQUEST["userId"];

$cartManager->addToCart($itemId, $userId);

有关准备好的语句的更多引用,您可以查看:http://php.net/manual/en/pdo.prepared-statements.php

关于javascript - 使用 POST 从 javascript 发送的数据插入到使用 php 的 mysql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49502590/

相关文章:

php - PHP 中 MySQL 函数的奇怪语法错误

php - 如何在MYSQL中存储用户购买?

php - 查询具有 md5 版本 id 的字段

javascript - CORS 错误 header 允许来源 * http 与 https

javascript - 将参数传递给 Raphael.js 中的 .mouseover 函数

php - 使用 PHP DOMPDF 和 AJAX 下载 PDF 文件

php - 在 PHP 中获取下一个/上一个 ISO 周和年份

php - 为什么 foreach 从查询结果中跳过 2 行?

javascript - 为什么我有这个 Uncaught TypeError : (0 , _normalizr.arrayOf) is not a function?

javascript - 如何使用 JS/jQuery 检查浏览器是否支持 touchstart?