PDO插入查询失败

时间:2013-07-15 18:42:55

标签: php mysql pdo

PHP:

    if(strcasecmp($refId, 'guest') == 0)
    {
        if(strcasecmp( $amount, '24.95' ) == 0)
        {
                $credits = 20;
                $plan = 'cool';     
        }
        if(strcasecmp( $amount, '45.95' ) == 0)
        {
            $credits = 40;
            $plan = 'awesome';
        }
        if(strcasecmp( $amount, '69.95' ) == 0)
        {
            $credits = 60;
            $plan = 'Supreme';
        }

            $keyy = generate_key_string();
            $query = $pdo->prepare("INSERT INTO keys (keys_key, keys_plan, keys_credit) VALUES (?, ?, ?)"); 
            $query->bindValue(1, $keyy);
            $query->bindValue(2, $plan);
            $query->bindValue(3, $credits);
            if(!$query->execute())
            {
                echo 'failed' . '<br/>';
                echo $plan . '<br/>';
                echo $keyy . '<br/>';
                echo $credits;

            }           
    }

SQL表:

CREATE TABLE `keys` (
  `keys_key` varchar(29) NOT NULL,
  `keys_credits` int(11) NOT NULL,
  `keys_plan` varchar(255) NOT NULL,
  PRIMARY KEY  (`keys_key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

我知道值存在,因为那是我的输出

failed
cool
CDGSI-DUEXP-M9BUZ-4VMQA-YSLIU
20

查询似乎不适用于此插入,我在其他页面上有不同的插入函数,它们工作正常。感谢

1 个答案:

答案 0 :(得分:4)

keys reserved keyword如果您通过反引号使用保留关键字,则应该转义您的列和表

像那样

    INSERT INTO `keys`