2014-10-01 90 views
2

我在php函數中擁有這些sql語句,並且已經傳遞了所有需要的變量。我在查詢中有2個插入語句。他們將數據插入到同一個表中,第一個插入用於主要服務,第二個插入如果在我的網站中使用附加服務。SQL不會從php中獲取所有傳遞的變量

SQL以某種方式獲取$ userid和$ item_label的值,第一個插入工作得很好。除非將$ userid和$ item_label更改爲靜態值,例如'123','122',否則第二次插入將不起作用。 (所以基本上,如果我離開他們作爲:userid:item_label,SQL不會值插入到表)

但是其他的重疊變量,如$filename,同時使用INSERT語句不會引起任何問題。

$userid不是目標表的索引或任何東西。所以,請幫助。這裏是代碼

function add_writing_editing_to_table($userid, $item_label, $basic_writing_service_id, $basic_writing_price, $editing_service_subcategory, $editing_service_id, $editing_serivce_price, $filename, $new_filename){ 

     global $db; 
     $query = "INSERT INTO op_cart 
       (service_category, userid, item_label, service_subcategory, service_id, is_addon, unit, total_price, filename, new_filename, checkout) 
     VALUES 
    ('writing', :userid, :item_label, 'basic_writing', :basic_writing_service_id, '0', '1', :basic_writing_price, :filename, :new_filename, '0'), 
    ('editing', :userid, :item_label, :editing_service_subcategory, :editing_service_id, '1', '1', :editing_serivce_price, :filename, :new_filename, '0');"; 
     $statement = $db->prepare($query); 
     $statement->bindValue(':userid', $userid); 
     $statement->bindValue(':item_label', $item_label); 
     $statement->bindValue(':basic_writing_service_id', $basic_writing_service_id); 
     $statement->bindValue(':basic_writing_price', $basic_writing_price); 
     $statement->bindValue(':filename', $filename); 
     $statement->bindValue(':new_filename', $new_filename);   
     $statement->bindValue(':editing_service_subcategory', $editing_service_subcategory); 
     $statement->bindValue(':editing_service_id', $editing_service_id); 
     $statement->bindValue(':editing_serivce_price', $editing_serivce_price); 
     $statement->execute(); 
     $statement->closeCursor(); 

} 
+0

可能重複http://stackoverflow.com/questions/15069962/php-pdo-insert-batch - 多行佔位符) – Ghost 2014-10-01 00:33:45

+0

好吧,如果userid和item_label沒有作爲變量傳遞到值的第二行,那麼可以實現多行插入....所以,我只想知道sql爲什麼會這樣做,如何解決它 – Bubble 2014-10-01 01:17:53

+0

我以某種方式設法解決了改變錯誤異常和傳遞值作爲字符串(以及大量的字符串函數),誘使sql認爲這兩個變量是靜態值... arr..it工作..但它是真的很傻=。= – Bubble 2014-10-01 03:20:46

回答

1

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

您必須包括要 傳中到語句中的每個值的唯一參數標記,當你調用PDOStatement對象::執行()。除非仿真模式打開,否則 不能在 準備好的語句中多次使用同一名稱的命名參數標記。

您需要重命名秒useriditem_label

[佔位符PHP PDO批量插入多個行(的
+0

謝謝。我其實是。它不起作用。但是,這個問題不會發生在$ filename和$ new_fileneame上,即使我使用了兩次....這是不是很奇怪? – Bubble 2014-10-01 01:29:56

+0

@泡泡,我稍後再測試一下。 – sectus 2014-10-01 01:31:40

+0

,如果我把這兩個插入到2個不同的功能,並一起調用它們。 SQL只會插入我稱之爲第一個的任何內容,而不會插入第二個。 – Bubble 2014-10-01 01:32:02