Javascript更新
$(document).ready(function(){$('select').on('change',function(){
var id = $(this).closest('tr').find('#idbase').val();
var price = $(this).closest('tr').find('#cost').val();
var quantity = $(this).closest('tr').find('select').val();
var sum = price * quantity;
var price = $(this).closest('tr').find('#total_qty').text(sum);
$.ajax({ // added {
url: "../Database/Query/EditOrder.php",
type: "POST",
data: {
'id': id,
'Quantity': quantity
},
success: function (data) {
alert(data);
}
}); // added }
});
});
EditOrder.php代码
<?php
require("../../Connection/Connect.php");
// php delete data in mysql database using PDO
if(isset($_POST['EditProduct']))
{
try {
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
} catch (PDOException $exc) {
echo $exc->getMessage();
exit();
}
// get id to delete
$id = $_POST['id'];
$Quantity = $_POST['quantity'];
$Subtotal = $_POST['subtotal'];
$sql = "UPDATE cart SET Quantity = :quantity WHERE id = :idbase";
$query = $conn->prepare($sql);
$result = $query->execute(array(":quantity"=>$Quantity, ":idbase"=>$id));
if($result){
header("Location: ../Public/Cart.php");
} else {
echo 'error';
}
}
?>
我尝试使用select的onblur来更新数据库并获取数量和总和的值以保存在数据库中,但我的数据库仍然没有更新我错过了什么?
答案 0 :(得分:-1)
试试这个
js code
$(document).ready(function(){$('select').on('change',function(){
var id = $(this).closest('tr').find('#idbase').val();
var price = $(this).closest('tr').find('#cost').val();
var quantity = $(this).closest('tr').find('select').val();
var sum = price * quantity;
var price = $(this).closest('tr').find('#total_qty').text(sum);
$.ajax({ // added {
url: "../Database/Query/EditOrder.php",
type: "POST",
data: {
'id': id,
'quantity': quantity,
'subtotal' : sum
},
success: function (data) {
alert(data);
}
}); // added }
});
});
php code
<?php
require("../../Connection/Connect.php"); // if this connect.php file contains your database connection then i guess you don't need to connect the data base again
if(isset($_POST['id']) && isset($_POST['quantity']) && isset($_POST['subtotal']))
{
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
$id = $_POST['id'];
$Quantity = $_POST['quantity'];
$Subtotal = $_POST['subtotal'];
$sql = "UPDATE cart SET Quantity = :quantity WHERE id = :idbase";
$query = $conn->prepare($sql);
$result = $query->execute(array(":quantity"=>$Quantity, ":idbase"=>$id));
if($result){
echo "updated";
} else {
echo 'error';
}
}
}
?>