数据库
首页 > 数据库> > PHP MySQL UPDATE语句不起作用

PHP MySQL UPDATE语句不起作用

作者:互联网

我有一个带有表(opendpu)的MySQL数据库,该表具有多个列,其中包括标题为“ ECRNUM”和“ PE_REQUIRED”的列.

我只是尝试通过指定一些值来测试此更新语句.我收到此错误:

Array ( [0] => 42000 [1] => 1064 [2] => 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 ‘DOE WHERE ECRNUM = 81308’ at
line 1 )

对于我的一生,我无法弄清楚这里出了什么问题.有人可以帮忙吗?

<?php
  require ('config.php');
 $ecrno = '81308';
 $pe_required = 'JOHN DOE';

while (true) {
try {
    $db = new PDO($dsn, $uname, $pword);
    $db->exec( "SET CHARACTER SET utf8" );
    $db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC ); 
    $db->setAttribute( PDO::ATTR_PERSISTENT, true );
    break;
}
    catch (Exception $e) {
        $db = null;
        $counter++;
        if ($counter == $limit)
            throw $e;
    }
}

$stmt = $db->prepare("UPDATE opendpu SET PE_REQUIRED = $pe_required WHERE ECRNUM = $ecrno");
$stmt->execute() or die(print_r($stmt->errorInfo(), true));

  ?>

.

解决方法:

像这样更改语法[变量的引号内]

$stmt = $db->prepare("UPDATE `opendpu` SET PE_REQUIRED = '$pe_required' WHERE ECRNUM = '$ecrno'");

标签:sql-update,mysql,php
来源: https://codeday.me/bug/20191030/1966117.html