dongqie2010 2014-03-20 09:46
浏览 68

php准备好的陈述|| 语法错误或访问冲突:1064

The code below is returning this error (Line numbers are in brackets[])

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' location, type, capacity, length, qty, serial, cert, lastinsp, inspby, datein, ' at line 1' in D:\Websitesiggingregister\pdo.php:90 Stack trace: #0 D:\Websitesiggingregister\pdo.php(90): PDOStatement->execute(Array) #1 {main} thrown in D:\Websitesiggingregister\pdo.php on line 90

I've edited the entry manually in PHPMyAdmin and the suggested 'PHP Code' came out as:

$sql = "UPDATE `riggingregister`.`register` SET `register` = \'100\',
`location` = \'testing\', `type` = \'Chains - 4 leg\', `capacity` = \'10mm\',
`length` = \'testing\', `qty` = \'testing\', `serial` = \'testing\', 
`cert` = \'testing\', `lastinsp` = \'testing\', `inspby` = \'testing\',
`datein` = \'testing\', `dateout` = \'testing\', 
`status` = \'HOLD\', `notes` = \'EDITED!\' WHERE `register`.`id` = 1;";

But I'm not sure how I can translate that into my prepared statement.

I've been reading tutorials, manuals and every possible question that SO offered me and can't see where I'm going wrong. Would love for someone to point me in the right direction.

Cheers

echo "the <b>CONFIRM CHANGES</b> button was pressed<br /><br />";

    $query = $dbh->prepare('UPDATE register SET register, location, type, capacity, length, qty, serial, cert, lastinsp, inspby, datein, dateout, status, notes VALUES ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE id = $id');
    $data = array($register, $location, $type, $capacity, $length, $qty, $serial, $cert, $lastinsp, $inspby, $datein, $dateout, $status, $notes);
    $query->execute($data);

    echo "Data has been written to the database!<br /><br />";
  • 写回答

2条回答 默认 最新

  • douzhanglun4482 2014-03-20 10:49
    关注

    Maybe something like

    echo "the <b>CONFIRM CHANGES</b> button was pressed<br /><br />";
    
    $data = array(
        'register' => $register,
        'location' => $location,
        'type' => $type,
        'capacity' => $capacity,
        'length' => $length,
        'qty' => $qty,
        'serial' => $serial,
        'cert' => $cert,
        'lastinsp' => $lastinsp,
        'inspby' => $inspby,
        'datein' => $datein,
        'dateout' => $dateout,
        'status' => $status,
        'notes' => $notes
    );
    
    $fieldDetails = NULL;
    
    foreach($data as $key=> $value) {
        $fieldDetails .= "`$key`=:$key,";
    }
    
    $fieldDetails = rtrim($fieldDetails, ',');
    
    $dbh = $this->prepare("UPDATE register SET $fieldDetails WHERE id = :id");
    
    foreach ($data as $key => $value) {
        $dbh->bindValue(":$key", $value);
    }
    
    $dbh->bindValue(":id", $id);
    
    if($dbh->execute()) {
    
         echo "Data has been written to the database!<br /><br />";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)