嘿伙计们,对于下面的代码,我很想得到您的帮助,我对 php 和 sql 还是很陌生,我正试图在订单结账过程中隐藏这些值。我需要从多个页面捕获信息......
我已经查看了这段代码几个小时,但我找不到哪里出错了……这可能是因为我真的不确定我需要在哪里解决这个问题。任何帮助或建议都会有很大帮助!
function writeOrderToDatabase(){
// open database connection
include 'includes/connection.php';
// store order date in Australian format for printouts etc
$_SESSION['orderDate'] = date('d-m-Y');
try{
// create our sql insert orders statement
$sql = "INSERT INTO orders SET orderNbr=: orderNbr,custNbr=:custNbr,orderDate=:orderDate, OrderNetValue=:OrderNetValue,deliverTo = :deliverTo,
deliveryAddress1 = :deliveryAddress1, deliveryAddress2 = :deliveryAddress2, deliverySuburb = :deliverySuburb,
deliveryState = :deliveryState, deliveryPostCode = :deliveryPostCode, deliverySuburb = :deliverySuburb, deliveryState = :state, deliveryPostCode = :deliveryPostCode, deliveryInstructions = :deliveryInstructions, shippingValue=:shippingValue,
paymentType=:paymentType, paymentRef=:paymentRef;";
// prepare the statement
$statement = $pdo->prepare($sql);
$orderNbr = 0;
// bind the values
$statement->bindValue(':orderDate', date('Y-m-d'));
$statement->bindValue(':custNbr', $_SESSION['custNbr']);
$statement->bindValue(':dispatchDate', $_SESSION['dispatchDate']);
$statement->bindValue(':deliveryDate', $_SESSION['deliveryDate']);
$statement->bindValue(':OrderNetValue', $_SESSION['OrderNetValue']);
$statement->bindValue(':deliverTo', $_SESSION['deliverTo']);
$statement->bindValue(':deliveryAddress1', $_SESSION['deliveryAddress1']);
$statement->bindValue(':deliveryAddress2', $_SESSION['deliveryAddress2']);
$statement->bindValue(':deliverySuburb', $_SESSION['deliverySuburb']);
$statement->bindValue(':deliveryState', $_SESSION['deliveryState']);
$statement->bindValue(':deliveryPostCode', $_SESSION['deliveryPostCode']);
$statement->bindValue(':deliveryInstructions', $_SESSION['deliveryInstructions']);
$statement->bindValue(':shippingValue', $_SESSION['shippingValue']);
$statement->bindValue(':paymentType', $_SESSION['paymentType']);
$statement->bindValue(':paymentRef', $_SESSION['paymentRef']);
$statement->bindValue(':sellingPrice', $_SESSION['sellingPrice']);
$statement->bindValue(':newQtyOnHand', $_SESSION['newQtyOnHand']);
// execute the statement
$success = $statement->execute();
} // end try
catch (PDOException $e) {
echo 'Error adding order: ' . $e->getMessage();
exit();
} // end catch
// test the result and get order nbr just created or display appropriate message
if ($success) {
echo $sql = 'SELECT orderNbr FROM orders ORDER BY orderNbr';
foreach ($conn->query($sql) as $row) {
print $row['orderNbr'] . "\t";
}
}
else {
die("<p>Unable to retreive Order Nbr </p>");
}
// read cart and insert orderedItem record(s) and update stock on hand in product records
foreach($_SESSION['cart'] as $prodNbr => $value) {
// store required details in variables
$qtyOrdered = $_SESSION['cart'][$prodNbr]['qtyOrdered'];
$qtyOnHand = $_SESSION['cart'][$prodNbr]['qtyOnHand'];
$sellingPrice = $_SESSION['cart'][$prodNbr]['price'];
try {
// create orderedItem table sql insert statement
$sql = "INSERT INTO orderedItem SET orderNbr=:custNbr,prodNbr=: prodNbr, qtyOrdered=:qtyOrdered,sellingPrice = :sellingPrice;";
} // end try
catch (PDOException $e) {
echo 'Error adding orderedItem: ' . $e->getMessage();
exit();
} // end catch
// test the result and display appropriate message
if (!$success) {
die("<p>Unable to execute the orderedItem table insert</p>");
}
// create new quantity on hand value for the product record
$newQtyOnHand = $qtyOnHand - $qtyOrdered;
try {
// create product table sql update statement
$sql="UPDATE product SET prodNbr= :prodNbr,prodName= :prodName,price= :price,qtyOnHand= :qtyOnHand,description= :description, photo= :photo,thumbNail= :thumbNail ,suppCode= :suppCode ;";
} // end try
catch (PDOException $e) {
echo 'Error updating product qtyOnHand: ' . $e->getMessage();
exit();
} // end catch
// test the result and display appropriate message
if (!$success) {
die("<p>Unable to execute the product table update</p>");
}
} // end of foreach
} // end of function