PHP - yeni oturum ürün ekleyin

2 Cevap php

Bir kullanıcı bir ürünü (onlar 3 ürün var diyelim) satın alıyor ve onların bilgi yazdığınız zaman, ürün otomatik olarak = 0 durumu ile veritabanına girer.

Kullanıcı uzakta sepetten gidiyor ise, (ve şimdi onlar sepette 4 ürün var, extra ürün satın alma) ürün genel olarak varsayalım.

Ben veritabanına yeni bir ürün eklemek yerine ürünlerin 3 zaten takılı olduğu için, tüm ürünleri + yeni bir ekleme nasıl bulmak zorunda.

$_SESSION['inserted_ids'] = array();
 		$id = '';	
        if( in_array( $id, $_SESSION['inserted_ids']) ){
             return;
        }else{

            $id = $db->Insert("INSERT INTO orders SET 
                       name    = '". $_SESSION['information']['name'] ."',
                       address = '". $_SESSION['information']['address'] ."',
                       date    = '". Database::Now() ."',
                       phone   = '". (isset($_SESSION['information']['phone']) ? $_SESSION['information']['phone']:'') ."',
                       email   = '".  $_SESSION['information']['email'] ."',
                       city    = '".  $_SESSION['information']['city'] ."',
                       zipcode = '".  $_SESSION['information']['zipcode']."'
            ");

            $_SESSION['inserted_ids'][] = (int) $id;


              # lists products
              $list = '';
              $grand_total = '';
              $res = $db->Execute("sql with product_id");
        		while($row = $res->GetNext()){


                    if( $row['product_id'] == $_SESSION['inserted_ids'] ){
                         $db->Execute("INSERT INTO order_lines SET 

                                          price         = '$round_price', 
                                          order_id      =  $id,
                                          product_id    =  $product_id, 
                                          product_name  = '$product_name',
                                          units         = '$grand_units', 
                                          status        = '0',
                                          date          = '".Database::Now()."', 
                                          item_num      = '".$item_num."';

                                          ON DUPLICATE KEY UPDATE SET 
                                              units = '$grand_units' 

                                          WHERE order_id = $id
                                          LIMIT 1;
                                      ");
                    }


                  }
        }

2 Cevap

Why don't you add a flag key to your product array that denotes whether the product was inserted or not, then before you insert the product, check the flag.

Bunu bile yapamıyorsan, o yerleştirildi ürün kimlikleri tutan (inserted_ids diyorlar) başka bir oturumu dizi oluşturun ve product_id {[olup olmadığını kontrol edin (0)]} dizisi.

(Sen sadece bunun için yepyeni bir döngü yapmak gerekmez, in_array'in kullanabilirsiniz).

Bu kod olmalı:

$_SESSION[ 'inserted_ids' ] = array(); // execute this line before everything else.

if( in_array( $id, $_SESSION[ 'inserted_ids' ] ) {
     return;
}
$id = $db->Insert("INSERT INTO orders SET 
               name    = '". $_SESSION['information']['name'] ."',
               address = '". $_SESSION['information']['address'] ."',
               date    = '". Database::Now() ."',
               phone   = '". (isset($_SESSION['information']['phone']) ? $_SESSION['information']['phone']:'') ."',
               email   = '".  $_SESSION['information']['email'] ."',
               city    = '".  $_SESSION['information']['city'] ."',
               zipcode = '".  $_SESSION['information']['zipcode']."'
   ");

$_SESSION['inserted_ids'][] = $id;

      $res = $db->Execute("sql statment");
while($row = $res->GetNext()){

 $db->Insert("INSERT INTO order_lines SET 

                          price         = '$round_price', 
                          order_id      = '".$id."',
                          product_id    = '$product_id', 
                          product_name  = '$product_name',
                          units         = '$grand_units', 
                          status        = '0',
                          date          = '".Database::Now()."', 
                          item_num      = '".$item_num."'
                ");
          }

order_lines tabloya bir UNIQUE kısıtlaması ekleme ve yinelenen bir bulunduğunda o satırı güncellemek:

ALTER TABLE order_lines ADD UNIQUE(order_id, product_id)

Sonra, bir order_line ON DUPLICATE KEY UPDATE deyimi kullandığınızda eklerken:

INSERT INTO order_lines SET
 price= 2.22,
 order_id= 2,
 product_id= 3,
 status = 0,
 ... other columns ...
ON DUPLICATE KEY UPDATE SET
 price= 2.22, 
 grand_unit= 4,
 ... other columns ...