2014-09-06 142 views
0

我遇到了问题,我的代码没有更新现有的计算机数据。如果我删除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"); 

?> 
+2

你忘了你的qoute更新值 - >'更新状态= '$状态',长度='$ length'' – Sean 2014-09-06 21:44:07

+0

肖恩 - 你是我的英雄。感谢您的修复 – MIchaelfjsmith2 2014-09-06 21:48:35

回答

0

一种更好的模式,用于创建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);