【问题标题】:ON DUPLICATE KEY UPDATE - Not updating重复密钥更新 - 不更新
【发布时间】:2014-10-31 12:52:44
【问题描述】:

我的代码无法更新现有计算机数据时遇到问题。如果我删除 ON Duplicate 部分,则代码可以正常工作并添加数据。我已将计算机作为我 xampp 数据库中的唯一密钥。任何帮助将不胜感激。

<?php

$receive = htmlspecialchars($_POST['time']);
list($length, $status, $computer) = split(":", $receive, 3);     
include('connection.php');


mysqli_query($dbc, "INSERT INTO screen(computer,status,length)
VALUES('$computer','$status','$length')
ON DUPLICATE KEY UPDATE 
status=$status, length=$length");

?>

【问题讨论】:

  • 你忘了列出你的更新值 -> UPDATE status='$status', length='$length'
  • 肖恩 - 你是我的英雄。感谢修复

标签: php mysql mysqli duplicates


【解决方案1】:

一种更好的创建 SQL 语句的模式,可以缓解一些常见的 SQL 注入漏洞。另请注意,如果插入成功,则可以使用特殊的 VALUES() 函数来引用将为列插入的值。

$sql = "INSERT INTO screen(computer,status,length)
VALUES('" 
. mysqli_real_escape_string($dbc,$computer)
. "','"
. mysqli_real_escape_string($dbc,$status)
. "','" 
. mysqli_real_escape_string($dbc,$length)
. "')
ON DUPLICATE KEY UPDATE 
status=VALUES(status), length=VALUES(length)";

mysqli_query($dbc,$sql);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2013-09-17
    • 2011-09-30
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多