PDO" ON DUPLICATE KEY UPDATE"准备好的声明

时间:2016-02-13 07:11:02

标签: php sql pdo

我正在尝试基于this thread准备好的声明。到目前为止,我收到错误消息SQLSTATE[HY000]: General error: 1 near "ON": syntax error以下是try块后面的查询:

$query = "INSERT INTO results2015_2016 ('id','ata','atc','atcommon','atn','ats','atsog','hta','htc','htcommon','htn','hts','htsog','bs','bsc','canationalbroadcasts','gcl','gcl1','gs','r1','usnationalbroadcasts')
          VALUES (':id',':ata',':atc',':atcommon',':atn',':ats',':atsog',':hta',':htc',':htcommon',':htn',':hts',':htsog',':bs',':bsc',':canationalbroadcasts',':gcl',':gcl1',':gs',':r1',':usNationalBroadcasts')
          ON DUPLICATE KEY UPDATE id= ':id2'";
try {   
    $db = new PDO('db info');  
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);  
    $stmt = $db->prepare($query);
    $stmt->bindParam(':ata', $ata, PDO::PARAM_STR);
    $stmt->bindParam(':atc' , $atc , PDO::PARAM_STR);
    $stmt->bindParam(':atcommon', $atCommon , PDO::PARAM_STR);
    $stmt->bindParam(':atn', $atn , PDO::PARAM_STR);
    $stmt->bindParam(':ats', $ats , PDO::PARAM_INT);
    $stmt->bindParam(':atsog', $atsog , PDO::PARAM_INT);
    $stmt->bindParam(':hta', $hta , PDO::PARAM_STR);
    $stmt->bindParam(':htc', $htc , PDO::PARAM_STR);
    $stmt->bindParam(':htcommon', $htCommon , PDO::PARAM_STR);
    $stmt->bindParam(':htn', $htn , PDO::PARAM_STR);
    $stmt->bindParam(':hts', $hts , PDO::PARAM_INT);
    $stmt->bindParam(':htsog', $htsog , PDO::PARAM_INT);
    $stmt->bindParam(':bs', $bs , PDO::PARAM_STR);
    $stmt->bindParam(':bsc', $bsc , PDO::PARAM_STR);
    $stmt->bindParam(':canationalbroadcasts', $caNationalBroadcasts , PDO::PARAM_STR);
    $stmt->bindParam(':gcl', $glc , PDO::PARAM_BOOL);
    $stmt->bindParam(':gcl1', $glc1 , PDO::PARAM_BOOL);
    $stmt->bindParam(':gs', $gs , PDO::PARAM_INT);      
    $stmt->bindParam(':r1', $r1 , PDO::PARAM_BOOL);
    $stmt->bindParam(':usnationalbroadcasts', $usNationalBroadcasts , PDO::PARAM_STR);
    $stmt->bindParam(':id', $idGame , PDO::PARAM_INT);
    $stmt->bindParam(':id2', $idGame , PDO::PARAM_INT);
    $stmt->execute();
} catch (Exception $e) {  
        echo $e->getMessage();
        exit;
    }   

我似乎无法对此错误消息及其与我的情况有什么关系。这段代码在解析jsonp的循环中......如果需要,我可以发布整个代码。

2 个答案:

答案 0 :(得分:1)

使用预准备语句时,您无需将参数括在任何类型的引号内。这将由发动机本身来处理:

$query = "INSERT INTO x(a,b,c,d) VALUES(:a, :b, :c, :d)";

答案 1 :(得分:-1)

正如@ HJPotter92所指出的那样 - 不应该引用sql中的占位符,还应该注意字段名称应该用反引号而不是单引号括起来(如果这些名称不被认为是保留的话,那么没有任何内容。& don' t包含空格等)

$query = "insert into results2015_2016 (
              `id`,`ata`,`atc`,`atcommon`,`atn`,`ats`,`atsog`,`hta`,
              `htc`,`htcommon`,`htn`,`hts`,`htsog`,`bs`,`bsc`,`canationalbroadcasts`,`gcl`,`gcl1`,
              `gs`,`r1`,`usnationalbroadcasts`
          ) values (
             :id,:ata,:atc,:atcommon,:atn,:ats,:atsog,:hta,:htc,:htcommon,:htn,:hts,
             :htsog,:bs,:bsc,:canationalbroadcasts,:gcl,:gcl1,:gs,:r1,:usnationalbroadcasts
          ) on duplicate key update id=:id2";