2009-08-27 14 views
0

私は、ユーザーがバスケットにアイテムを置くためのスクリプトを書いています。これまでのところ非常に複雑で、誰かと話をして、より良いデザインを提案したり、現在のデザインを整理したりできるかどうかを確認したいと思います。ここでは非常にうまく機能していない私が持っているコード、(つまり、私はまだ解決されていないエラーがある)は、私の意思を示すために、コメントと、次のとおりです。バスケットスクリプトに追加 - いくつかのデザインヘルプ、してください

<?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です。検索結果と製品ビューページが機能し、同じサーバーを使用しているため、このスクリプトと同じデータベースです。

+1

auch。私の目が痛い...まず:エディタにコードとしてコードを書く。秒:できるだけ私たちにあなたの問題を助けるために必要な少しのコードを与えてみてください...これはちょうどInformationOverflowです – peirix

+0

私はコードボックスになるように編集しようとしていました。私はそれが長いスクリプトだと知っていますが、私は戦略の一般的な意見と、このコードがうまくいくべきかどうかを知る必要があるので、すべてを見る必要があると感じています。 – user97410

答えて

1

PHPの組み込み疑似オブジェクト指向スタイルの使用を検討する必要があります。

また、ZendやCakePHPなどのPHPフレームワークの使用を検討する必要があります。 PHPフレームワークを持っていなくても、phpのクラスとインターフェイスオブジェクトを使ってオブジェクト指向のコードを作成することができます。

このコードをクラスと関数に分けることで、現在と将来のある時点でコードを編集するときに、(そして私たちの)デバッグをずっと簡単にすることができます。

+0

私は非常にプログラミングに新しいので、オブジェクト指向までのステップは、私はむしろこのプロジェクトでは避けることが遅れている。私は現時点で手続き型のスタイルにうまく乗りますが、私はそのことを覚えています。このプロジェクトの後、私は私が働いているすべての言語の知識と応用を進めたいと思います.OOPは次の論理的な動きのようです。 – user97410

関連する問題