简体   繁体   中英

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. 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)

 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)

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

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

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. D'oh!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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