2012-06-04 29 views
-1

这里是我的SQL语句:我的PDO插入语句有什么问题?

protected static $_SQLInsert = "INSERT INTO location 
(host_id, street, suburb, region, post_code, country, phone, email, 
timezone, longitude, latitude, is_main) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

我准备的声明如下:

static::$_PDOSInsert = self::$_PDO->prepare(static::$_SQLInsert); 

准备与12个值的数组后,我执行的语句:

static::$_PDOSInsert->execute($array); 

我然后得到以下警告:

PDOStatement对象::执行():SQLSTATE [HY093]:无效的参数号:参数是不...

那么,我究竟做错了定义???

编辑:这里的数组:

(
    [host_id] => 15 
    [street] => Street 15 
    [suburb] => Suburb 15 
    [region] => Region 15 
    [post_code] => Post Code 15 
    [country] => AU 
    [phone] => 12341234 
    [email] => [email protected] 
    [timezone] => 1 
    [longitude] => 123 
    [latitude] => 234 
    [is_main] => 1 
) 

谢谢!

+1

可能重复[“无效参数编号:参数未定义”插入Yii数据] stackoverflow.com/questions/5874383/invalid-parameter-number-parameter-was-not-defined-inserting-data-in-yii) – mario

+1

'参数未定义',也许你忘了在数组中定义一个参数?我会使用关联占位符作为':host_id'而不是几十个'''来更好地跟踪你的参数。 –

+0

@mario:我没有使用命名参数 –

回答

2

pdo对于你给execute()的数组真的很挑剔。如果您使用了未命名的占位符(如?),它需要一个数字索引的数组,并且它必须从索引0开始。

+0

谢谢克里斯,这是一个重要的学习! –