php - 添加到篮子脚本 - 请提供一些设计帮助

标签 php mysql shopping-cart shopping

我正在编写一个脚本,让用户将元素放在他们的购物篮中。到目前为止它非常复杂,我想与某人讨论一下,看看他们是否可以提出更好的设计或整理当前的设计。这是我的代码,它运行得不是很好(即有我尚未解决的错误),并带有注释以表明我的意图:

<?php
session_start();
include_once("db_include.php5");
doDB();


if(!$_GET["productid"] || !$_GET["qty"]) {
//the user has entered the address directly into their address bar, send them away (if=1 to let me know where the script branched)
header("Location:index.php5?if=1");
exit();
}

**//do select query to verify item id is valid, in case they entered data into the query string or the item has been removed from db**
$check_sql = "SELECT * FROM aromaProducts1 WHERE id='".$_GET["productid"]."'";
$check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli));

if(mysqli_num_rows($check_res) == 0) {
**//item doesn't exist, redirect user**
header("Location:index.php5?if=2");
exit();
} else if(mysqli_num_rows($check_res) != 0) {
**//item exists
//do select query to check for item id already in basket - if this item is already in the table associated with the user's session id (which will be added every time an item is), then we want to change the quantity only**
$duplicate_sql = "SELECT qty FROM sessionBasket WHERE product_id='".$_GET["productid"]."' AND usersessid='".$_SESSION["PHPSESSID"]."'";
$duplicate_res = mysqli_query($mysqli, $duplicate_sql) or die(mysqli_error($mysqli));

if(mysqli_num_rows($duplicate_res) != 0) {
**//item in basket - add another - fetch current quantity and add new quantity**
$basketInfo = mysqli_fetch_array($duplicate_res);
$currQty = $basket_info["qty"];
$add_sql = "UPDATE sessionBasket SET qty='".($_GET["qty"]+$currQty)."' WHERE usersessid='".$_SESSION["PHPSESSID"]."'AND product_id='".$_GET["productid"]."'";
$add_res = mysqli_query($mysqli, $add_sql) or die(mysqli_error($mysqli));

if($add_res !== TRUE) {
**//wasn't updated for some reason - this is where my script currently breaks**
header("Location:basketfailredirect.php5?error=add");
exit();
} else if($add_res === TRUE) {
**//was updated - send them away**
header("basket.php5?res=add");
exit();
}


} else if(mysqli_num_rows($duplicate_res) == 0) {
**//no existing items in basket, so we want to add the current item info associated with the user's id/session id**

**//fetch product id, passed in query string from the product info page**
$productid = $_GET["productid"];

**//sanitize possible inputs, if set - notes is a field added to the product info page for custom products, and we want to sanitize it if it's set - note that check_chars_mailto() is a function I have in the db_include file**
$notes = isset($_GET["notes"])?trim(mysqli_real_escape_string(check_chars_mailto($_GET["notes"]))):"";
**//if the user is logged in, their userid is stored in the session variable**
$userid = $_SESSION["userid"]?$_SESSION["userid"]:"";
**//not sure about the keep alive option - i.e. store basket contents even if the user doesnt register/sign in, but keeping the option there**
$alive = $_SESSION["alive"]?$_SESSION["alive"]:"no";


**//insert query**
$insert_sql = "INSERT INTO sessionBasket (userid, usersessid, date_added, keep_alive, product_id, qty, notes) VALUES (
'".$userid."',
'".$_SESSION["PHPSESSID"]."',
now(),
'".$alive."',
'".$productid."',
'".$_GET["qty"]."',
'".htmlspecialchars($notes)."')";
$insert_res = mysqli_query($mysqli, $insert_sql) or die(mysqli_error($mysqli));

if($insert_res === TRUE) {
**//success**
header("Location:basket.php5?res=add");
exit();
} else if($insert_res !== TRUE) {
**//fail**
header("Location:basketfailredirect.php5?error=add2");
exit();
}
}
}
?>

这对我来说非常复杂 - 我想允许空字段,如果它可用则添加用户 ID(我的 UPDATE 查询中缺少)......这是一个好的设计一百万英里,还是什么?

此外,当我尝试将商品添加到购物篮时,现在出现错误:内部服务器错误 500。我怀疑这是由于编码错误,因为我的搜索结果和产品 View 页面正常工作,并且它们使用相同的服务器和相同的数据库作为这个脚本。

最佳答案

您应该考虑使用 PHP 的内置伪面向对象样式。

您或许还应该考虑使用 PHP 框架,例如 Zend 或 CakePHP。即使您不使用 PHP 框架,您仍然应该能够通过 php 的类和接口(interface)对象以面向对象的方式创建代码。

通过将此代码分离到类和函数中,您将使您的(和我们的)调试变得更加容易,无论是现在还是将来您要编辑代码时。

关于php - 添加到篮子脚本 - 请提供一些设计帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1340994/

相关文章:

asp.net - 稍后在购物车中给我开账单

php - 如何杀死一个/所有 php session ?

php - DOCUMENT_ROOT 不完整,缺少域文件夹

php - 连接两个表时出现 "Message : Undefined Variable:query "错误 codeigniter mysql php

mysql - 如何使用sequelize mysql添加外键

php - 选择 mysql 查询中的前一个值

Codeigniter 购物车 : Adding an item to cart more than once replaces

encryption - HTTP 发布 : getting encrypted Paypal multiple-item cart upload button

php - 三张表的数据库连接查询

php - SQL 备份无法与 PHP 脚本一起使用