【问题标题】:insert and update record插入和更新记录
【发布时间】:2013-02-06 09:28:16
【问题描述】:

您好,谁能帮帮我...我已经尝试了几种不同的方法,同时试图实现这样的输出。

id   idnumber    datein      timein     dateout     timeout

1     123      2013-02-21   08:00:01   2013-02-21   11:12:45

2     456      2013-02-21   10:15:01   2013-02-21   05:30:01

3     123      2013-02-21   06:58:52   2013-02-21   03:20:16

4     123      2013-02-21   10:05:35   2013-02-21   

但是我仍然无法得到它...我正在尝试进行出勤监控,只要将其记录到数据库中,一个人就可以按他/她的意愿计时和超时。我在 php 中有这两个代码可以工作,但不是我想要的输出。

下面的代码只是插入一条记录,但更新不起作用:

if (isset($_POST['idnumber'])){
      $query = "INSERT INTO tblattendance SET idnumber='".$_POST['idnumber']."', datein=CURDATE(), timein = CURTIME()";
      $insert_result = mysql_query($query);
}else{
      $query1 = "UPDATE tblattendance SET dateout=CURDATE(), timeout = CURTIME() WHERE idnumber='".$_POST['idnumber']."' ORDER BY id DESC LIMIT 1 ";
      $insert_result1 = mysql_query($query1);   
}

下面的代码仅在没有idnumber的现有记录时执行插入和更新,但如果idnumber存在,它只是更新超时列。

$query = "SELECT * FROM tblattendance WHERE idnumber='".$_POST[idnumber']."'";
$res = mysql_query($query);
list($exist) = mysql_fetch_array($res);

    if (!$exist){
          $query = "INSERT INTO tblattendance SET idnumber='".$_POST['idnumber']."', datein=CURDATE(), timein = CURTIME()";
          $insert_result = mysql_query($query);
    }else{
         $query1 = "UPDATE tblattendance SET dateout=CURDATE(), timeout = CURTIME() WHERE idnumber='".$_POST['idnumber']."' ORDER BY id DESC LIMIT 1 ";
         $insert_result1 = mysql_query($query1);    
    }

【问题讨论】:

  • 抱歉不明白你的问题。在一个短语中,您提到“下面的代码仅插入一条记录,但更新不起作用:”而在另一个短语中,您说“下面的代码仅在没有 idnumber 的现有记录但如果 idnumber存在,它只是更新超时列。” ..那么究竟是什么情况和错误??
  • 不要在更新语句中使用 ORDER BY。并尝试理解插入语句。 INSERT INTO .. SET x = y 仅适用于 MySQL,没有标准 SQL 语言。
  • 提防sql注入,mysql_*也被贬低了
  • 我只是输入了我尝试过的代码......所有这些代码并没有真正给出我想要的输出。你能给我至少插入更新的正确代码吗?因为在 php 方面我并不是真正的专家。

标签: php mysql insert-update


【解决方案1】:

原始代码尝试使用UPDATE 语法(而不是一般的INSERT 语法,尽管基于此source 似乎是正确的)插入数据库。

      $query = "INSERT INTO tblattendance SET idnumber='".$_POST['idnumber']."', datein=CURDATE(), timein = CURTIME()";

对于我的示例,我使用下面的表架构...

CREATE TABLE IF NOT EXISTS `tblattendance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `idnumber` int(11) NOT NULL,
  `timein` datetime DEFAULT NULL,
  `datein` datetime DEFAULT NULL,
  `timeout` datetime DEFAULT NULL,
  `dateout` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

删除了rhein从原始代码中的一些重复

//grab the id_number
$id_number = $_POST['idnumber'];
//fetch rows
$query = "SELECT * FROM test.tblattendance WHERE idnumber='".$id_number."'";
$res = mysql_query($query);
$rows = mysql_fetch_array($res);
//create an initial query to update the database
$query = "UPDATE test.tblattendance SET dateout=CURDATE(), timeout = CURTIME() WHERE idnumber='".$id_number."'";
//check we have no rows
if (empty($rows)){
      //because we have no rows modify the query
      $query = "INSERT INTO test.tblattendance VALUES('', $id_number, CURDATE(), CURDATE(), null, null);";
} @mysql_query($query);

为了有效对抗 SQL 注入。来自$_POST 的所有元素都应该用mysqli_real_escape_string 包裹,或者您应该在查询数据库之前将$_POST 中的撇号替换为ASCII 字符。

【讨论】:

  • 如果我想用新的 timein 和 timeout 再次记录相同的 IDNumber 怎么办?
  • 我试过了,但它只是更新了超时列。我想要的是在更新超时列之后,如果我再次发布 IDNumber 它将插入到具有新时间的新行中?
  • @rhein 您在更新数据库时使用了不正确的语法。
  • 我已经尝试过您的代码,先生,但它对我不起作用...正如您在我的表格中看到的那样,我有一个自动递增的字段,并且我有一个 IDNumber 字段。所以 id 和 idnumber 列不一样。
  • @rhein 我已经修改了答案中的查询以适合您的table schema(尽管将来您应该从链接here 中学习插入,我在 w3schools 上提供了正确的语法) .
猜你喜欢
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多