2017-09-15 6 views
-1

나는 작업 카트 (아래 코드 참조)를 가지고 있지만 'dbexample'데이터베이스의 '주문'MYSQL 테이블에 내용을 저장하고 싶습니다. 'order_number'및 'order_status'필드에 추가되었습니다. 그러면 주문 추적/상태 시스템을 만들 수 있습니다.MYSQL 테이블에 장바구니 내용을 저장하는 방법

내 카트은 다음과 같습니다 를

<?php 
session_start(); 
include_once("config.php"); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>View shopping cart</title> 
<link href="style/style.css" rel="stylesheet" type="text/css"></head> 
<body> 
<h1 align="center">View Cart</h1> 
<div class="cart-view-table-back"> 
<form method="post" action="cart_update.php"> 
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Price</th><th>Total</th><th>Remove</th></tr></thead> 
    <tbody> 
    <?php 
    if(isset($_SESSION["cart_products"])) //check session var 
    { 
     $total = 0; //set initial total value 
     $b = 0; //var for zebra stripe table 
     foreach ($_SESSION["cart_products"] as $cart_itm) 
     { 
      //set variables to use in content below 
      $product_name = $cart_itm["product_name"]; 
      $product_qty = $cart_itm["product_qty"]; 
      $product_price = $cart_itm["product_price"]; 
      $product_code = $cart_itm["product_code"]; 
      $product_color = $cart_itm["product_color"]; 
      $subtotal = ($product_price * $product_qty); //calculate Price x Qty 

      $bg_color = ($b++%2==1) ? 'odd' : 'even'; //class for zebra stripe 
      echo '<tr class="'.$bg_color.'">'; 
      echo '<td><input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>'; 
      echo '<td>'.$product_name.'</td>'; 
      echo '<td>'.$currency.$product_price.'</td>'; 
      echo '<td>'.$currency.$subtotal.'</td>'; 
      echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /></td>'; 
      echo '</tr>'; 
      $total = ($total + $subtotal); //add subtotal to total var 
     } 

     $grand_total = $total + $shipping_cost; //grand total including shipping cost 
     foreach($taxes as $key => $value){ //list and calculate all taxes in array 
       $tax_amount  = round($total * ($value/100)); 
       $tax_item[$key] = $tax_amount; 
       $grand_total = $grand_total + $tax_amount; //add tax val to grand total 
     } 

     $list_tax  = ''; 
     foreach($tax_item as $key => $value){ //List all taxes 
      $list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'<br />'; 
     } 
     $shipping_cost = ($shipping_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $shipping_cost).'<br />':''; 
    } 
    ?> 
    <tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?></span></td></tr> 
    <tr><td colspan="5"><a href="index.php" class="button">Add More Items</a><button type="submit">Update</button></td></tr> 
    <a href="place-order.php" ><img src="images/buynow.jpg" width="179" 
    </tbody> 
</table> 
<input type="hidden" name="return_url" value="<?php 
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); 
echo $current_url; ?>" /> 
</form> 
</div> 

</body> 
</html> 

을 시험 삼아 내가이 복사합니다 (내가 장소-order.php을 열려는 "지금 구매"코드가 어디로 가야 그건 함께..) 페이지 order.php 장소가 전화 만 추가 :이 너무 작동

$sql = "INSERT INTO orders (product_code, product_name, product_price, product_qty) 
VALUES ('$product_code', '$product_name', '$product_price', '$product_qty')"; 

if ($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

있지만 ONE 제품은 '주문'테이블에 추가됩니다. "for each"또는 무엇인가로해야한다고 생각합니까? 시도했지만 많이 작동하지 못했습니다. 나를 올바른 방향으로 안내 할 수있는 사람은 누구입니까?

+0

[리틀 바비] (http://bobby-tables.com/)라고 ***입니다 http://php.net/manual/en/mysqli.quickstart.prepared-statements.php SQL Injection Attack 위험이 있습니다.] (http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) *** [준비된] (http : // en.wikipedia.org/wiki/Prepared_statement) 문구 [MySQLi] (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). 심지어 [이스케이프 문자열] (http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) 안전하지 않습니다! –

답변

0

구문을 사용하면 실제로 이와 같이 여러 행을 삽입 할 수 있습니다.

INSERT INTO tableName 
    (column1, column2, column3, column4) 
VALUES 
    ('value1', 'value2', 'value3', 'value4'), 
    ('value1', 'value2', 'value3', 'value4'), 
    ('value1', 'value2', 'value3', 'value4'); 

부분합과 합계를 계산하는 기존 foreach 루프에서이를 실행할 수 있습니다.

SQL 쿼리에서 위생을하지 않고 변수를 사용하면 심각한 보안 문제가 발생한다는 점을 잊지 마십시오. mysqli 또는 PDO에 대한 예를 들어 준비된 쿼리를 사용하여 자신을 보호 할 수 있습니다. 당신은 여기에 대한 자세한 내용을보실 수 있습니다 :

How can I prevent SQL injection in PHP?

http://php.net/manual/en/book.pdo.php

가 [스크립트가