ON DUPLICATE KEY UPDATE无效

时间:2014-12-12 15:17:33

标签: mysql

它不断添加新行而不是更新旧行:

$tbl_shifttimes = "CREATE TABLE IF NOT EXISTS shifttimes (
            id INT(11) NOT NULL AUTO_INCREMENT,

            date DATE NOT NULL,
            firstname VARCHAR(20) NOT NULL,
            lastname VARCHAR(20) NOT NULL,
            firm VARCHAR(30),
            starttime TIME NOT NULL,
            endtime TIME NOT NULL,
            hours INT(10),

            PRIMARY KEY (id),
            UNIQUE KEY (firstname, lastname, firm, date)
            )";

这是我正在运行的查询:

if(isset($_POST["firstname"]) && isset($_POST["lastname"]) && isset($_POST["firm"]) && isset($_POST["starttime"]) && isset($_POST["endtime"])) {
    $firm = $_POST["firm"];
    $starttime = $_POST["starttime"];
    $endtime = $_POST["endtime"];
    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $date = $_POST["date"];
    $sql = "INSERT INTO shifttimes (firstname, lastname, firm, date, starttime, endtime)       
            VALUES('$firstname', '$lastname', '$firm', '$date', '$starttime', '$endtime') 
            ON DUPLICATE KEY UPDATE firstname='$firstname', lastname='$lastname', firm='$firm', date='$date', starttime='$starttime', endtime='$endtime'";
    $query = mysqli_query($db_conx, $sql); 
}

知道我做错了什么吗?这是我得到的表:

7  2014-12-22  Niclas  Morgenstern  TestBusiness  08:00:00  22:00:00  NULL
6  2014-12-22  Niclas  Morgenstern  TestBusiness  08:00:00  16:00:00  NULL

0 个答案:

没有答案