user3793272
user3793272

Reputation: 111

updating a table with foreign key

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

time table

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

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

Here is my code in time in

$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?

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;

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>";

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

Upvotes: 2

Views: 93

Answers (1)

Mawia HL
Mawia HL

Reputation: 3665

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

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

Upvotes: 1

Related Questions