【问题标题】:Error updating table with INSERT ... ON DUPLICATE KEY UPDATE使用 INSERT 更新表时出错 ... ON DUPLICATE KEY UPDATE
【发布时间】:2018-06-04 15:36:39
【问题描述】:

我对 mySQL 比较陌生。出于某种原因,我的 INSERT ON DUPLICATE KEY UPDATE 命令出现错误。这是它的样子:

print "Writing status to file \n";
$mysqliNode = new mysqli('blah.net', 'us', 'pw', "CATV_Mon");
$stmtNode = $mysqliNode->prepare("INSERT INTO CATV_Mon.nodeDetail (nodID, pctRedYellow, count)
                                  VALUES (?,?,?) 
                                  ON DUPLICATE KEY UPDATE pctRedYellow   = VALUES(percentRedYellow)
                                                         ,count     = VALUES(count)                               
                                  ");
$index = "ABCF";
$tempCount = 5000; 
$node_percent_down = 55;
$stmtNode->bind_param("sss",$index,$node_percent_down,$tempCount);
if(!$stmtNode->execute) {
        $tempErr = "Error getting Node $nod info and put in table: " . $stmtNode->error;
        printf($tempErr . "\n");    //show mysql execute error if exists  
        $err->logThis($tempErr);
        printf ("will die on purpose now \n");  
        die();          
} //if stmtNode didn't execute ok

当我运行它时,我看到(它没有显示错误消息):

Writing status to file
Error getting Node WILL info and put in table:  
will die on purpose now

我尝试使用 MySQL Workbench 中的语句:

INSERT INTO CATV_Mon.nodeDetail (nodID, pctRedYellow, count)
                                  VALUES ("ABCE",30,5000) 
                                  ON DUPLICATE KEY UPDATE pctRedYellow   = VALUES(pctRedYellow)
                                                         ,count     = VALUES(count) ;

这行得通,并且说有 1 行受到影响,我在表格中看到了它。当我更改值以更新并在工作台中运行它时,它会更新更改的内容并且在表格中看起来不错。

任何想法为什么它在 php 脚本中失败?我尝试更改要插入的 nodeID,但同样失败。

表结构为: nodID varchar(20) 主键 pctRedYellow int(5) 计数 int(8)

我在网上搜索,看起来我正在做我应该做的事情。 insert on duplicate key update。我在想我的语句中需要 WHERE,但它在工作台中工作,并且与链接上的语法看起来不错。

【问题讨论】:

  • bind_param("sss",$index,$node_percent_down,$tempCount); 变量$node_percent_down$tempCount 是整数,所以请尝试bind_param("sii",$index,$node_percent_down,$tempCount);,而不是像手册中所说的表“类型规范字符”“我对应的变量具有整数类型”源php.net/manual/en/mysqli-stmt.bind-param.php
  • 在您拥有printf ("will die on purpose now \n"); 的位置也输出错误消息,然后编辑您的问题以显示完整的错误文本
  • 我试过 sii 还是有同样的问题:Writing status to file Error getting Node WILL info and put in table: will die on purpose now
  • @SloanThrasher - 这很奇怪......它没有在那里打印错误消息。

标签: php mysql insert on-duplicate-key


【解决方案1】:

我找到了这个,它修复了它。 bind param doesn't work

$stmtNode->bind_param('sii', $index, $node_percent_down, $tempCount);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2018-06-27
    • 2013-10-03
    • 1970-01-01
    • 2015-07-30
    • 2011-02-12
    • 1970-01-01
    相关资源
    最近更新 更多