我在记录timein和timeout
时有这段代码if(isset($_POST['submit']))
{
$query=mysql_query("Select * FROM tblattendance where IDNumber='" . $_POST['IDNumber'] . "' ");
list($exists) = mysql_fetch_row($query);
$idnumber=$row['IDNumber'];
$fname=$row['FirstName'];
$mname=$row['MiddleName'];
$lname=$row['LastName'];
if($exists){
$sql="UPDATE $tbl_name SET TimeoutAM=CURTIME() WHERE IDNumber='" . $_POST['IDNumber'] . "' ORDER BY Date DESC, TimeinAM DESC LIMIT 1";
}else{
$sql="INSERT INTO $tbl_name SET Date=CURRENT_DATE(), TimeinAM=CURTIME(), IDNumber='$idnumber', FirstName='$fname', MiddleName='$mname', LastName='$lname' ";
}
$result=mysql_query($sql);
}
我的问题是,如果IDNumber不存在,它将添加一个带有timein的新记录,然后更新超时。但是,当我尝试再次添加相同的IDNumber时,它没有添加新记录,而只是更新现有记录的超时列。如何再次添加记录呢?
答案 0 :(得分:0)
INSERT ... ON DUPLICATE KEY UPDATE Syntax。这将插入或更新。
查询:
$idnumber = $_POST['IDNumber'];
INSERT INTO $tbl_name(IDNumber,Date,Timein,FirstName,MiddleName,LastName)
values( '$idnumber',CURRENT_DATE(), NOW(), '$fname', =$mname', '$lname'
ON DUPLICATE KEY UPDATE Timeout=CURRENT_DATE()
您的第一列必须是IDNumber,因为它遵循列的顺序来创建过滤器,更新将解释如下:
UPDATE $tbl_name SET Timeout=NOW() WHERE IDNumber='" . $_POST['IDNumber'] . "
代码:
<?php
$sql="INSERT INTO $tbl_name(IDNumber,Date,Timein,FirstName,MiddleName,LastName)
values( '$idnumber',CURRENT_DATE(), NOW(), '$fname', =$mname', '$lname'
ON DUPLICATE KEY UPDATE Timeout=CURRENT_DATE()";
$result=mysql_query($sql);
?>