У меня проблемы с корзиной PHP. Я следовал учебному курсу на sanwebe.com.PHP Ошибка корзины PHP
На моей странице показаны мои продукты и показаны тележки, которые могут быть обновлены отлично, после чего я создал окончательную страницу заказа для того, чтобы клиент заполнил их данные о доставке и детали оплаты, и я хотел, чтобы мой скрипт отправил всю эту информацию, так как а также элементы в корзине для таблицы в моей базе данных.
Я могу отправить данные с помощью оператора SQL, но проблема в том, что когда клиент покупает продукт 1 и продукт 2, он отправляет данные только для первого продукта в корзину. Так что, если человек покупает 3 продукта 1, он отправит его поверх штрафа, но при смешивании разных продуктов он терпит неудачу.
Я думаю, может быть, должна быть какая-то форма цикла или что-то, чтобы передать несколько предметов?
order.php
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function() {
$('#orderform').validate(); // Select the form using its idname and apply the validate() function to it.
}); // end ready()
</script>
<body>
<div id="products-wrapper">
<h1>Complete Order</h1>
<div class="order">
<form id="orderform"action="process_order.php" method="post">
<h2>Delivery</h2>
<p><label for="firstname">First Name:</label> <input type="text" name="firstname" class="required"/></p>
<p><label for="lastname">Last Name:</label> <input type="text" name="lastname" class="required"/></p>
<p><label for="email">E-mail:</label> <input type="text" name="email" class="required email"/></p>
<p><label for="address">Address:</label> <input type="text" name="address" class="required" /></p>
<p><label for="city">City:</label> <input type="text" name="city" class="required" /></p>
<p><label for="postcode">Post Code:</label> <input type="text" name="postcode" class="required" /></p>
<p><label for="country">Country:</label> <input type="text" name="country" class="required" /></p>
<h2>Payment</h2>
<p><label for="cardnumber">Card Number:</label> <input type="text" name="cardnumber" class="required"/></p>
<p><label for="expire">Expiration Date:</label> <input type="text" name="expire" class="required digits"/></p>
<p><label for="scode">Security Code:</label> <input type="text" name="scode" class="required digits"/></p>
<p class="submit"><input type="submit" value="Submit" /></p>
</div>
<div class="shopping-cart">
<h2>Your Order</h2>
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<ul>';
$cart_items = 0;
foreach ($_SESSION["products"] as $cart_itm)
{
$product_code = $cart_itm["code"];
$results = $mysqli->query("SELECT product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT 5");
$obj = $results->fetch_object();
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>';
echo '<div class="p-price">'.$currency.$obj->price.'</div>';
echo '<div class="product-info">';
echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
echo '<div class="p-qty"><strong>Qty : '.$cart_itm["qty"].'</strong></div>';
echo '<div>'.$obj->product_desc.'</div>';
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
$vat = ($total * '20'/'100');
$ordertotal = ($total + $vat);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
echo '<input type="hidden" name="ordertotal['.$cart_items.']" value="'.$ordertotal.'" />';
$cart_items ++;
}
echo '</ul>';
echo '<span class="check-out-txt">';
echo '+VAT : '.$currency.$vat.' ';
echo '<br>';
echo '<strong>Total : '.$currency.$ordertotal.'</strong> ';
echo '<br>';
echo '</span>';
echo '</form>';
}else{
echo 'Your Cart is empty';
}
?>
</div>
</body>
</html>
processorder.php
<!-- File: process_order.php
Job: PHP script to handle the information submitted to it by the webpage named testform.html
-->
<?php
session_start();
include_once("config.php");
?>
<html>
<head>
<title>Form processing script</title>
</head>
<body>
<?php
// The form contents are held in a special array named $POST.
// The next statements assign the information held in $POST to PHP variables for processing
// Delivery
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$address = $_POST["address"];
$city = $_POST["city"];
$postcode = $_POST["postcode"];
$country = $_POST["country"];
$cardnumber = $_POST["cardnumber"];
$expire = $_POST["expire"];
$scode = $_POST["scode"];
// Order
$product_name = $_POST["item_name"][0];
$product_code = $_POST["item_code"][0];
$product_qty = $_POST["item_qty"][0];
$ordertotal = $_POST["ordertotal"][0];
// Make a connection to the MySQL database server
$dbserverIP="localhost";
$dbusername="root"; // Use your own name
$dbuserpassword=""; // Use your own password
$connection = mysql_connect($dbserverIP,$dbusername,$dbuserpassword) or die("Couldn't connect to the dbserver.");
// Make a connection to the database
$dbname="ecomm"; // Use your own database name
$dbselectok = mysql_select_db($dbname,$connection) or die("Couldn't select the remote database.");
// Issue an SQL INSERT INTO command to the MySQL RDBMS
$sqlstatement = "INSERT INTO `ecomm`.`order` (`firstname`, `lastname`, `email`, `address`, `city`, `postcode`, `country`, `cardnumber`, `expire`, `scode`, `product_name`, `product_code`, `product_qty`, `ordertotal`)
VALUES ('$firstname', '$lastname', '$email', '$address', '$city', '$postcode', '$country', '$cardnumber', '$expire', '$scode', '$product_name', '$product_code', '$product_qty', '$ordertotal')";
$sql_result = mysql_query($sqlstatement,$connection) or die("Couldn't execute the SQL INSERT statement");
// Issue an SQL SELECT command to the MySQL RDBMS
$sqlstatement = "SELECT * FROM `order`";
$sql_result = mysql_query($sqlstatement,$connection) or die("Couldn't execute the SQL SELECT statement");
// Process the information retrieved from the database and display to the user's browser
while ($row = mysql_fetch_array($sql_result))
{
$fn = $row["firstname"];
$ln = $row["lastname"];
$em = $row["email"];
echo "<BR>$fn $ln $em\n";
}
// Free up any memory holding the database records
mysql_free_result($sql_result);
// Close the connection to the database server
mysql_close($connection);
?>
</body>
</html>
У вас есть хотя бы одна проблема с архитектурой базы данных, которую я вижу. –
Не могли бы вы объяснить, что это за проблема? Спасибо за ваше время! – user3573531
см. Ответ ниже –