2014-10-01 65 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