DannyM Posted February 28, 2008 Share Posted February 28, 2008 So, I have gone through and revamped some of my code, and made it cleaner and easier for me to read. However, now when I click on a button, it does not update the database? It only updates the entries for amWork/amBreak.. Here is my code - controlPanel.php <?php //Require the two PHP file to validate the user require("connect.php"); require("validate.php"); //Variables $minute=gmdate("i"); $hour=gmdate("G"); $day=gmdate("j"); $month=gmdate("M"); //See what they clicked. if(isset($_POST['CheckIn'])) { echo 'Check In<br/>'; clockIn(); } if(isset($_POST["CheckOut"])) { echo 'Check Out<br/>'; clockOut(); calcTime(); } if(isset($_POST["BreakStart"])) { echo 'Break Start <br/>'; breakStart(); } if(isset($_POST["BreakEnd"])) { echo 'Break End <br/>'; breakEnd(); } //Echo the body of the page. echo 'Hello, ' . $_COOKIE["Name"] . ". What are you going to do today?"; echo '<br/>'; echo '<br/>'; //Echo the form of the page echo " <form method='POST' action='controlPanel.php'> <input type='submit' name='CheckIn' value='Check In'>   <input type='submit' name='CheckOut' value='Check Out'>   <input type='submit' name='BreakStart' value='Go On Break'>   <input type='submit' name='BreakEnd' value='Return from Break'> </form>"; //Functions function clockIn() { $sql=mysql_query("UPDATE employees SET amWork='1' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET workStartH='$hour' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET workStartM='$minute' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET dayStart='$day' WHERE ID='$_COOKIE[iD]'"); } function clockOut() { $sql=mysql_query("UPDATE employees SET amWork='0' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET workEndH='$hour' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET workEndM='$minute' WHERE ID='$_COOKIE[iD]'"); } function breakStart() { $sql=mysql_query("UPDATE employees SET amBreak='1' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET breakStartH='$hour' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET breakStartM='$min' WHERE ID='$_COOKIE[iD]'"); } function breakEnd() { $sql=mysql_query("UPDATE employees SET amBreak='0' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET breakEndH='$hour' WHERE ID='$_COOKIE[iD]'"); $sql=mysql_query("UPDATE employees SET breakEndM=$min WHERE ID='$_COOKIE[iD]'"); } function calcTime() { $result=mysql_query("SELECT * FROM employees WHERE ID='$_COOKIE[iD]'"); while($row=mysql_fetch_array($result)) { $hourS=$row['workStartH']; $breakHS=$row['breakStartH']; $breakHE=$row['breakEndH']; $hourE=$row['workEndH']; $minS=$row['workStartM']; $breakMS=$row['breakStartM']; $minE=$row['workEndM']; $date=$row['dayStart']; if($date != $day) { $mult = $day-$date; $mult = $mult * 24; $min=60-abs(0-$minS); $min=$min+$minE; $dayTotal = $mult - $hourS; $dayTotal += $hourE; $dayTotal -= ($breakHE - $breakHS); echo $dayTotal . ":" .$min; } else { $min=60-abs(0-$minS); $min=$min+$minE; $dayTotal = $hourE - $hourS; echo $dayTotal . ":" .$min; } } } ?> validate.php <?php if(!$_COOKIE["ID"]) { header("Location: login.php"); } ?> connect.php <?php //Attempt to establish a connection $con=mysql_connect("X","-","-"); if(!$con) { die("Could not connect to the network."); } //Attempt to connect to the database $db=mysql_select_db("-",$con); if(!$db) { die("Could not connect to the database."); } ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted February 28, 2008 Share Posted February 28, 2008 fist thing first ,..... your function should at the top of your page right before you call them. eg.. function here then call function here you do it on the other way you will end up having undefined function message ! Quote Link to comment Share on other sites More sharing options...
DannyM Posted February 28, 2008 Author Share Posted February 28, 2008 fist thing first ,..... your function should at the top of your page right before you call them. eg.. function here then call function here you do it on the other way you will end up having undefined function message ! Hm, okay, thanks. I fixed that up. Any idea why it won't update the database, though? Quote Link to comment Share on other sites More sharing options...
teng84 Posted February 28, 2008 Share Posted February 28, 2008 try your update rga function clockOut(){ $query = "UPDATE employees SET amBreak='0', breakEndH='$hour', breakEndM=$min WHERE ID='{$_COOKIE[iD]}'"; mysql_query($query ) or die(mysql_error()); } try something like that and see the error! Quote Link to comment Share on other sites More sharing options...
awpti Posted February 28, 2008 Share Posted February 28, 2008 fist thing first ,..... your function should at the top of your page right before you call them. eg.. function here then call function here you do it on the other way you will end up having undefined function message ! Totally untrue. The script is compiled and then executed by the engine. Function definitions can be anywhere. Quote Link to comment Share on other sites More sharing options...
DannyM Posted February 28, 2008 Author Share Posted February 28, 2008 fist thing first ,..... your function should at the top of your page right before you call them. eg.. function here then call function here you do it on the other way you will end up having undefined function message ! Totally untrue. The script is compiled and then executed by the engine. Function definitions can be anywhere. Here's the direct error - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID='0001'' at line 1 But, the more I look at my MySQL syntax, the more it looks right? Quote Link to comment Share on other sites More sharing options...
awpti Posted February 28, 2008 Share Posted February 28, 2008 Post the entire query. Quote Link to comment Share on other sites More sharing options...
haku Posted February 28, 2008 Share Posted February 28, 2008 You have a lot of sql queries, so I don't know which one you were getting the error on, but I suspect it may be this one: $sql=mysql_query("UPDATE employees SET breakEndM=$min WHERE ID='$_COOKIE[iD]'"); You didn't set single quotes around $min like you did with all the other queries. Quote Link to comment Share on other sites More sharing options...
DannyM Posted February 28, 2008 Author Share Posted February 28, 2008 Ah, ok. I got that error to go away, thanks Haku. But, that still hasn't fixed my main error, the database is not updating itself As per request by awpti, here are all of my queries. mysql_query("UPDATE employees SET amWork='1' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET workStartH='$hour' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET workStartM='$minute' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET dayStart='$day' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET amWork='0' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET workEndH='$hour' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET workEndM='$minute' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET amBreak='1' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET breakStartH='$hour' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET breakStartM='$min' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET amBreak='0' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET breakEndH='$hour' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); mysql_query("UPDATE employees SET breakEndM='$min' WHERE ID='$_COOKIE[iD]'") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
revraz Posted February 28, 2008 Share Posted February 28, 2008 make sure $_COOKIE[iD] contains a value (INT I assume?) and put { } around it. If you echo the query, I'm sure you'll see the problem because something will be missing. Quote Link to comment Share on other sites More sharing options...
DannyM Posted February 28, 2008 Author Share Posted February 28, 2008 Well, I did some more experimentation. It appears that when I call the functions, they have no clue as to what $hour,$day, or $minute are. Which is strange, I've never had that problem before. However, if I insert the variables within the function itself, it works correctly. Any idea on how to fix this? Is there a way to make variables "global"? I thought all variables were set to that by default, but... Quote Link to comment Share on other sites More sharing options...
premiso Posted February 28, 2008 Share Posted February 28, 2008 <?php //Require the two PHP file to validate the user require("connect.php"); require("validate.php"); //Variables global $minute,$hour,$day,$month; $minute=gmdate("i"); $hour=gmdate("G"); $day=gmdate("j"); $month=gmdate("M"); .... //Functions function clockIn() { global $minute,$hour,$day,$month; Functions do not recognize variables that are not marked as global and are not in their scope. Making the variables global like above should allow you to use them in functions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.