Just wanted to know if someone can help me with this code. I can't see where it fails.
case "3":
echo "
<h2 align=\"center\">Arbeidstid - Start/Stop</h2>";
$today = date("Y-m-d H:i:s");
$userid = $_COOKIE['userid'];
echo "<form action=\"index.php?fid=3\" method=\"post\">";
if (isset($_POST['today'])) {
if (isset($_COOKIE['wstart'])) {
$query=$oDB->Prepare("UPDATE workhours SET stop=:today WHERE userid=:userid");
unset($_COOKIE['wstart']);
setcookie('wstart', '', time() - 3600); //delete or reset cookie
}
else {
$query=$oDB->Prepare("INSERT INTO workhours (userid, start) VALUES (:userid, :today)");
setcookie("wstart", $time, time()+96000);
}
$query->execute(array(':userid' => $userid, ':today' => $today));
header("location:index.php");
if (!$query) {
echo "\nPDO::errorInfo():\n";
print_r($oDB->errorInfo());
}
}
else {
if (isset($_COOKIE['wstart'])) {
echo "<input type=\"hidden\" name=\"today\" value=\"$today\"><input type=\"submit\" value=\"stop\" />";
}
else {
echo "<input type=\"hidden\" name=\"today\" value=\"$today\"><input type=\"submit\" value=\"start\" />";
}
}
echo "</form>";
break;
It supposed to show a stop button if the cookie wstart is set and if not the button should say start.
Stop button will execute a different query than start cause I want the data value into the stop column and the start value into the start column. What happens is that is shows the start button all the time.
UPDATED CODE THAT ALMOST WORKS EXCEPT FROM THE DELETE COOKIE PART
case "3":
echo "
<h2 align=\"center\">Arbeidstid - Start/Stop</h2>";
$today = date("Y-m-d H:i:s");
$userid = $_COOKIE['userid'];
echo "<form action=\"index.php?fid=3\" method=\"post\">";
if (isset($_POST['today'])) {
if (isset($_COOKIE['wstart'])) {
$query=$oDB->Prepare("UPDATE workhours SET stop=:today WHERE userid=:userid ORDER BY start DESC LIMIT 1");
unset($_COOKIE['wstart']);
setcookie('wstart', null, -1, '/'); //delete or reset cookie
}
else {
$query=$oDB->Prepare("INSERT INTO workhours (userid, start) VALUES (:userid, :today)");
setcookie("wstart", $today, time()+96000);
}
$query->execute(array(':userid' => $userid, ':today' => $today));
header("location:index.php");
if (!$query) {
echo "\nPDO::errorInfo():\n";
print_r($oDB->errorInfo());
}
}
elseif (!isset($_POST['today'])) {
if (isset($_COOKIE['wstart'])) {
echo "<input type=\"hidden\" name=\"today\" value=\"$today\"><input type=\"submit\" value=\"stop\" />";
}
else {
echo "<input type=\"hidden\" name=\"today\" value=\"$today\"><input type=\"submit\" value=\"start\" />";
}
}
else { echo "yeah yeah";}
echo "</form>";
break;
Thanks for reading!