2012-11-28 3 views
0

나는 대학 프로젝트 및 튜터를위한 장바구니를 만들고 있는데 샘플 코드가 있지만 내 디자인에 맞게 조정하고 있습니다. 쇼핑 카트 PHP 디스플레이 오류

내 사이트 제품

이 같은

cutecupcak.es/product.php?id=vanilla_cupcakes 

오히려

cutecupcak.es/product.php?id=2 

보다 더 장바구니에 제품을 표시하는 장바구니 페이지의 코드로 이름이 지정됩니다

if(is_numeric($id)) { 
    $cartresult = mysql_query($sql); 

하지만, 내 제품 URL이 숫자가 아니므로 여기에서 볼 수 있듯이 장바구니에 결과가 표시되지 않습니다. - http://cutecupcak.es/shoppingcart.php - 테스트하는 경우.

당신이

전에서 변경하려면 어떻게해야합니까

하지만 products.php?id=2 등이있는 경우가 작동합니다 - 내 URL 형식으로 작동하도록 is_numeric?

전체 코드

<?php 

session_start(); // start new session or call exisiting session 
include('include/dbconnect.php'); // include the database connections 

function renderCartForm() 
    { 
    $cart = $_SESSION['cart']; 
    if ($cart) { 
     $items = explode(',',$cart); 
     $contents = array(); 
     foreach ($items as $item) { 
      $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; 
     } 
     $output[] = '<form action="shoppingcart.php?action=update" method="post" id="cart">'; 
     $output[] = '<div class="form">'; 
     $output[] = '<table border="2" class="formcontainer">'; 
     $output[] = '<tr class="formlabels">'; 
     $output[] = '<td width="400">Cake Name</td>'; 
     $output[] = '<td width="75">Price</td>'; 
     $output[] = '<td width="75">Quantity</td>'; 
     $output[] = '<td width="100">Total</td>'; 
     $output[] = '<td width="100">Remove?</td>'; 
     $output[] = '</tr>'; 
     foreach ($contents as $id=>$qty) { 
      $sql = 'SELECT * FROM `CAKE` WHERE `cake_url` = '.$id; 

      if(is_numeric($id)) { 
       $cartresult=mysql_query($sql); 

      while($cartrow = mysql_fetch_array($cartresult)) { 
       $output[] = '<tr>'; 
       $output[] = '<td>'.$cartrow['cake_name'].'</td>'; 
       $output[] = '<td>'.$cartrow['cake_price'].'</td>'; 
       $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="2" maxlength="2" /></td>'; 
       $output[] = '<td>&pound;'.($cartrow['cake_price'] * $qty).'</td>'; 
       $output[] = '<td><a href="shoppingcart.php?action=delete&id='.$id.'">Remove</a></td>'; 
       $total += $cartrow['cake_price'] * $qty; 
       $output[] = '</tr>'; 
      } 
      } 
     } 
     $output[] = '</table>'; 
     $output[] = '</div>'; 
     $output[] = '<p>Grand total: <strong>&pound;'.$total.'</strong></p>'; 
     $output[] = '<div class="\"formlabels\"><button type="submit">Update Cart</button></div>'; 
     $output[] = '</form>'; 
    } else { 
     $output[] = '<p>Your shopping cart is empty.</p>'; 
    } 
    echo join(' 
       ',$output); 
    } 

?>

+0

제품 이름의 형식은 무엇입니까? 당신의 예에는 문자와 밑줄이 있지만 숫자는 가질 수 있습니까? 아포스트로피? 인용 부호? – andrewsi

+0

모든 URL은 vanilla_cupcakes와 같은 숫자가 아니며 기본적으로 한 단어는 하나의 단어를 밑줄로 표시합니다. – FoamyMedia

+0

이 경우 해당 텍스트가 해당 형식인지 확인하는 함수를 작성할 수 있습니다. – andrewsi

답변

0

PHP의 is_string 기능이 당신을 위해 무엇을 찾고 있습니다. 숫자가 포함 된 제품 이름이없는 경우 !is_numeric을 간단히 입력 할 수도 있습니다.

+0

(1)'is_numeric ('abc123def')는 문자열에 숫자가 있더라도'false'를 반환합니다. (2) 이론적으로'$ id'가 비어 있지 않다는 것을 제외하고는 테스트를 할 필요가 전혀 없습니다. 그러나 그의 SQL 코드는 변경해야합니다. –

+0

사실, ID가 비어 있지 않은지 확인하기 위해 테스트를 변경합니다. –

+0

! is_numeric doesnot work가 throw되고 오류가 발생합니다. 알아낼 수 있다면 전체 코드를 업로드 했습니까? 많은 감사 – FoamyMedia

0

$id이 숫자가 아닌 문자열 인 경우 숫자가 아닌지 여부를 확인할 필요가 없습니다. 사실, 만, 비어 있지 여부 확인 그러나 같은

if(strlen(trim($id)) { 
    $cartresult = mysql_query($sql); 

필요!$sql의 빌드 방법을 거의 수정해야합니다. 코드에 표시하지는 않지만, mysql_query 행 전에 수행해야합니다. 당신은 아마이 예제 오히려 어쨌든되지 않습니다 기능의 mysql_ 세트,보다 PDO를 사용하고 있음을

$dbh = new PDO('mysql:host=hostname;port=portnum;dbname=databasename', 'username', 'password'); 

if(strlen(trim($id)) { 
    $sql = "SELECT * FROM product WHERE id=?"; 
    $sth = $dbh->prepare($sql); 
    $sth->bindParam(1, $id, PDO::PARAM_ISTR); 
    $sth->execute(); 

    while($row = $sth->fetch()) { 
     ... 
    } 
} 

주 같은 것을하게 될 겁니다. PDO는 훨씬 더 다재다능하고 안전합니다 (제대로 사용 된 경우).