2012-02-29 179 views
0

用戶想要購買let'say 30項,是否有其他方式比for循環?如何提出多個請求MYSQL

我怎樣才能讓30個插入

public function buy($item_id) 
{ 
    $price = $this->input->post('price'); 
    $amount = $this->input->post('amount'); 
    $sum = $this->input->post('sum'); 
    $price = intval($price); 
    $amount = intval($amount); 
    $sum = intval($sum); 

    if(!is_numeric($price) || !is_numeric($amount) || !is_numeric($sum)) 
    { 
     exit("Неверные значения"); 
     return false; 
    } 

    if(!empty($_SESSION['id'])) 
    { 
     $user_id = $_SESSION['id']; 
    } 
    else 
    { 
     echo 'not logged'; 
     exit(); 
    } 
    $q = $this->db->query("SELECT * FROM users WHERE id = ".$user_id); 
    $t = $this->db->query("SELECT * FROM items WHERE id = ".$item_id." AND 
    TO_DAYS(end) - TO_DAYS(now()) >= 0"); 
    if($this->db->affected_rows() != 0) 
    { 
     $user = $q->result_array(); 
     $item = $t->result_array(); 
     $active = $user[0]['active']; 
     $user_id = $user[0]['id']; 
     $money = $user[0]['money']; 
     $price = $item[0]['price']; 
     $bought_times = $item[0]['bought_times']; 
     $coupons = $item[0]['coupons']; 
     $discount = $item[0]['discount']; 
     $short_desc = $item[0]['short_desc']; 
     $status = ""; 

     //echo $coupons; 
     //exit(); 
     for($i = 0; $i < $amount; $i++) 
     { 
      if($coupons <= 0 || ($amount > $coupons)) 
      { 
       $status = 'out of coupons'; 
       break; 

      } 
      if($money >= $price) 
      { 
       $money = $money - $price; 
       $this->db->query("INSERT INTO user_items (`discount`, `short_desc`, `user_id`) 
       VALUES(".$discount.", '".$short_desc."', ".$user_id.")"); 
       $this->db->query("UPDATE users SET money = ".$money." WHERE id = ".$user_id); 
       $coupons -= 1; 
       $this->db->query("UPDATE items SET coupons = ".$coupons." WHERE id = ".$item_id); 
       $q = $this->db->query("SELECT * FROM users WHERE id = ".$user_id); 
       $user = $q->result_array(); 
       $money = $user[0]['money']; 
       $_SESSION['money'] = $money; 
       $status = 'buy successfull'; 
       //redirect(''); 
      } 
      else 
      { 
       // 
       $status = 'not enough money'; 
       break; 

      } 

     } 
     switch($status) 
     { 
      case 'out of coupons': 
      echo 'out of coupons'; 
      break; 

      case 'buy successfull': 
      $bought_times += 1; 
      $this->db->query("UPDATE items SET bought_times = ".$bought_times." WHERE id = ".$item_id); 
      echo 'buy successfull'; 
      break; 

      case 'not enough money': 
      echo 'not enough money'; 
      break; 
     } 
     /*foreach($q->result_array() as $u); 
     { 
      $_SESSION['id'] = $u[0]->id; 
      $_SESSION['email'] = $u[0]->email; 
      redirect(''); 

     }*/ 
     //return 'trolol'; 
    } 
    else 
    { 
     echo 'out of time'; 
    } 
    //echo $user_id; 
} 

這是控制器Ajax請求,在我的本地機器上所有的好,但在託管其給出了一個錯誤。

我需要減去資金覈減優惠券,並插入一行取決於用戶量

你可以給我這個代碼的建議嗎?

+0

換句話說,我的意思是我怎樣才能優化這個代碼?,我不知道如何使正確的插入項目的權利 – 2012-02-29 12:51:43

+0

爲什麼不使用codeigniter購物車類? – 2012-02-29 13:01:39

回答

1

讓你的查詢更加動態有一個打擊查詢只嘗試使用可能產生一個查詢類似的循環進行的連接:在這種方式

INSERT INTO my_table(column1, column2, column3) VALUES ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3') ... 

你就可以扔只是一個查詢,但將多個項插入到表中,只需用逗號(,)分隔每組值。

+0

但我怎樣才能使它在一個循環?我怎麼能把這些逗號之間的循環?如果它取決於數量 – 2012-02-29 12:57:13

+1

「但是我怎樣才能使它在循環?」 =>使用foreach循環來連接sql查詢的值部分 – Rooneyl 2012-02-29 13:00:51

+0

感謝您爲這個想法提供的很多! – 2012-02-29 13:28:22