我正在寫一個腳本,讓用戶放置物品在他們的籃子。到目前爲止,這是非常複雜的,我想和他人談一談,看看他們能否提出更好的設計或者整理當前的設計。這裏是我的代碼,這並不能很好地工作(即有,我還沒有得到解決的錯誤),以留言中顯示我的意圖:放入購物車腳本 - 一些設計的幫助,請
<?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();
}
}
}
?>
這實在是太複雜了,我 - 我想允許空字段,添加用戶標識符(如果可用的話)(這在我的UPDATE查詢中是缺失的)......這是距離一個好設計百萬英里還是什麼?
此外,當我嘗試將項目添加到購物籃時,我現在得到一個錯誤:內部服務器錯誤500.我懷疑這是由於編碼錯誤,因爲我的搜索結果和產品查看頁面工作,並且他們使用相同的服務器和這個腳本一樣的數據庫。
auch。我的眼睛受傷......首先:在編輯器中將代碼編寫爲代碼。第二:儘可能給我們一些代碼,以幫助解決您的問題......這只是InformationOverflow – peirix 2009-08-27 13:10:47
我打算將它編輯在代碼框中,呃,對不起。我知道這是一個很長的腳本,但我需要一個關於策略的一般意見,以及這段代碼是否應該工作,所以我覺得有必要看看這一切。 – user97410 2009-08-27 13:16:52