简体   繁体   English

为什么我的AJAX调用不会插入数据库?

[英]Why won't my AJAX call insert to the database?

I have an ajax function that I'm using to store information in the database. 我有一个ajax函数,用于在数据库中存储信息。 The success message goes off in the console log, but nothing actually gets stored. 成功消息在控制台日志中消失,但实际上没有任何存储。 Any ideas? 有任何想法吗?

If you want me to turn on error messages, please tell me exactly how to do this, and in which file. 如果要我打开错误消息,请告诉我确切的操作方法以及在哪个文件中。

AJAX (goes off on a button click) AJAX(单击按钮即可关闭)

 var id = ranNum;
    //.cart-content is an item inside a shopping cart
    $('.cart-content').each(function() {
        //I know this is not the best way to grab data, but for now it's what I have to do
        var item = $(this).find('.item').text();
        var price = $(this).find('.price').text();

        //These return the proper text, so I'm grabbing the information fine.
        console.log("id: " + id);
        console.log("item: " + item);
        console.log("price: " + price);

        $.ajax({
            url : '../controllers/insertOrder.php',
            //I even added extra letters just in case I couldn't use the same word twice.
            data: {idd: id, itemm: item, pricee: price},
            type : 'POST',
            success : function(){console.log("success");},
            error : errorHandler
        });

    });

insertOrder.php (the path is correct) insertOrder.php(路径正确)

<?php
require_once '../../models/database.php';
require_once 'Order.php';

$order = new Order;
$order->insertOrder($_POST['idd'], $_POST['itemm'], $_POST['pricee']);

Order.php Order.php

class Order{

    public function __construct() {

    }

    public function insertOrder ($id, $item, $price) {

        $db = Dbclass::getDB();
        $query = "INSERT INTO orders (order_id, order_item, order_price)
               VALUES(:id, :item, :price)";          

        $statement = $db->prepare($query);
        $statement->bindParam(':id', $id, PDO::PARAM_INT, 11);
        $statement->bindParam(':item', $item, PDO::PARAM_STR, 50);
        $statement->bindParam(':price', $price, PDO::PARAM_STR, 10);

        $statement->execute();

    }

}

Figured it out. 弄清楚了。 My path to the database in my insertOrder.php file wasn't right. 我的insertOrder.php文件中的数据库路径不正确。 D'oh! 天哪!

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

相关问题 我的 AJAX 调用只返回我的数据库的一部分,不会在滚动时加载更多 - My AJAX call only returns partial of my database and won't load more on scroll 为什么我的Ajax AutoSave无法正常工作? - Why won't my ajax AutoSave work? 对PHP函数的AJAX调用不会使它为我的数据库输入值 - AJAX call on PHP function won't make it enter a value to my Database 为什么这个jQuery Ajax调用在Chrome中不起作用 - Why won't this jQuery ajax call work in chrome 我的 JavaScript 代码在 jquery ajax 调用之后不会运行 - My JavaScript code won't run after jquery ajax call IE为什么无法识别AJAX调用中返回的JavaScript代码? - Why won't IE recognize javascript code returned in AJAX call? 为什么这种形式不会调用JS函数? (试图获取使用PHP脚本和AJAX调用MySQL数据库的自动完成表单字段) - Why this form won't call the JS function? (trying to get an autocomplete form field calling a MySQL database with a PHP script and AJAX) 为什么我的 ajax 调用不在数据库中存储数据? 没有 PHP 或控制台错误 - Why doesn't my ajax call store data in database? No PHP or console errors 为什么我的 QuillJS Delta 不能插入空格? - Why won't my QuillJS Delta insert a space? 为什么我的javascript不会发布或提交到我的数据库? - Why my javascript won't post or submit to my database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM