[英]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.