2009-08-27 5 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(); 
} 
} 
} 
?> 

: 여기 내 의도를 보여주고 의견 (즉, 내가 아직 해결되지 않은 오류가) 아주 잘 작동하지 않는, 내가 가지고있는 코드입니다 빈 필드를 허용하고 사용 가능한 경우 userid를 추가하십시오 (업데이트 쿼리에서 누락되었습니다) ... 이것은 좋은 디자인에서 수백만 마일입니까? 아니면 무엇입니까?

또한 바구니에 항목을 추가하려고하면 내부 서버 오류 500이 발생합니다. 검색 결과 및 제품보기 페이지가 작동하고 동일한 서버를 사용하기 때문에 이것이 잘못된 코딩으로 인한 것 같습니다. 이 스크립트와 동일한 데이터베이스

+1

auch. 내 눈이 아프다 ... 먼저 코드를 편집기에 코드로 쓴다. 두 번째 : 가능한 한 당신의 문제를 돕기 위해 필요한 약간의 코드를 제공 해보십시오 ... 이것은 단지 InformationOverflow입니다 – peirix

+0

나는 그것을 코드 상자에 넣기 위해 편집하려고했습니다, ㅎ, 미안 해요. 나는 그것이 긴 스크립트라는 것을 알고 있지만 전략에 대한 일반적인 견해와이 코드가 작동해야하는지 여부를 알아야하므로 모든 것을 볼 필요가 있다고 느낍니다. – user97410

답변

1

PHP의 내장 의사 객체 지향 스타일을 사용해야합니다.

Zend 또는 CakePHP와 같은 PHP 프레임 워크를 사용해야 할 수도 있습니다. PHP 프레임 워크를 사용하지 않더라도 PHP의 클래스 및 인터페이스 객체를 통해 객체 지향적 인 방식으로 코드를 작성할 수 있어야합니다.

이 코드를 클래스와 함수로 분리하면 현재와 미래의 어느 시점에서 코드를 편집 할 때 훨씬 쉽게 디버깅 할 수 있습니다.

+0

프로그래밍에 익숙하지 않으므로 객체 지향 단계는이 프로젝트에서 피할 수있는 지연입니다. 나는 그 절차 적 스타일을 잘 알고 있지만, 나는 그 생각을 명심 할 것이다. 이 프로젝트가 끝나면 나는 내가 사용하고있는 모든 언어에 대한 지식과 응용을 향상시키고 싶다. 그리고 OOP가 다음 논리적 인 움직임으로 보인다. – user97410