本文介绍了用外键更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了时间超时系统.这是表

I created a time in and time out system. Here is the table

时间表

| id | time id | time in | time out |
|  1 |    1    | 1:00 am |          |
|  1 |    2    | 1:013am |          |

id 是外键,而 time id时间表 主键.如何更新超时表?

id is a foreign key while the time id is a time table primary key. How can i update the timeout table?

这是我时间

$id = $_GET['id'];
$timein = date('Y/m/d H.i.s');

$sql = "INSERT INTO time (id, timein)
        VALUES
        ('$id','$timein')
";
$query = mysql_query($sql);

mysql_close();

if(is_resource($query) and mysql_num_rows($query)>0){
    $row = mysql_fetch_array($query);
    echo $row["id"];
    }
echo $sql;

这是正确的,但我的问题是如何更新超时?

which is correct, but my question is how can I update the time out?

这将针对特定的时间 ID,例如时间 ID 1.但我也想更新超时.

This will target the certain time id, for example time id 1. But I also want to update time out.

我已经尝试过这样的事情

I already tried something like this

$timeid = $_SESSION['timeid'];
$id = $_GET['id'];
$timein = $_POST['timein'];
$timeout = date('Y/m/d H.i.s');

$sql = "UPDATE time SET(time_id, id, timein, timeout)
        VALUES
        ('$time_id','$id','$timein','$timeout') WHERE id = $id
";
$query = mysql_query($sql);

mysql_close();

if(is_resource($query) and mysql_num_rows($query)>0){
    $row = mysql_fetch_array($query);
    echo $row["id"];
    }
echo $sql;

这是用于查看时间和超时上下文的代码,那么我如何获得确切的时间 ID 来编辑它?

and this is the code is use to view the context of time in and time out so how can i get the exact timeid to edit it?

include_once 'dbconfig.php';
session_start();
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id";
$result = mysql_query($sql);
?>
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><center>".$row['time_id']."</center></td>";
echo "<td><center>".$row['timein']."</center></td>";
echo "<td><center>".$row['timeout']."</center></td>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "<td><a href= \"timeout.php?id=".$row['time_id']." \"> timeout";
echo "</tr>";

多亏了 MAWIA,更新没问题,但是当我更新时,只有一个 timeid 会更新

the update is ok thanks to MAWIA but when i update only one timeid will get updated

推荐答案

如果你想更新超时,我认为你不需要更新 time_id、id 和 timein,因为你已经在你的第一个语句中插入了.所以你的更新语句是:

If you want to update a timeout, I think you don't need to update time_id, id and timein because you already insert in your first statement. So your update statement would be:

   $checkFk = mysql_query("SET FOREIGN_KEY_CHECKS=0");//turn off foreign key
   $sql = "UPDATE `time` SET `timeout`='$timeout' WHERE `timeid` = '$timeid'";
   $resetFk = mysql_query("SET FOREIGN_KEY_CHECKS=1");//turn on foreign key

不要使用mysql扩展请看mysql改进扩展pdo

Don't use mysql extension Please look at mysql improved extension or pdo

这篇关于用外键更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:36