0

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!

  • If post variable today is set, you never display the button, only if it's not set – pinkfloydx33 Apr 19 '14 at 12:25
  • yes to execute the sql query, but if its not set it will go into a new if elseif where it should show me the start or stop button depending on the cookie- wstart is set. – Roger Andersen Apr 19 '14 at 12:28
  • Point being, are sure today variable isn't set? – pinkfloydx33 Apr 19 '14 at 12:28
  • When you delete unset the cookie. Try to unset based on this rules http://stackoverflow.com/questions/686155/remove-a-cookie , because i think the cookie will always be set – Paul Bele Apr 19 '14 at 12:30
  • I though the cookie check if else statement should be able to detect a cookie or not. And if there is a cookie the "stop" button should appear instead. – Roger Andersen Apr 19 '14 at 12:34
  • Hi Roger, I agree with Pinkfloydx33. Now you need to trace your code by yourself, check every time in which condition your pointer will go. If you have further query let me access your full code, so that i'll check it and solved out. Thanks – Keyur Mistry Apr 19 '14 at 12:38
  • Hi there, I have almost made it work now. Updated and added the working code. Still have a problem to delete the cookie and turn the stop button back to start.. Thanks :) – Roger Andersen Apr 19 '14 at 12:50

1 Answers1

0

I think you have an error here :

    setcookie("wstart", $time, time()+96000);           

If $time is defined previously, please make sure it has a value before setting it in cookie $time is not defined anywhere, if you define it, it will validate the if and' and output the 'stop' button

Paul Bele
  • 1,514
  • 1
  • 11
  • 12