Jump to content

Unable to Update database?


DannyM

Recommended Posts

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'>
&nbsp
<input type='submit' name='CheckOut' value='Check Out'>
&nbsp
<input type='submit' name='BreakStart' value='Go On Break'>
&nbsp
<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.");
}




?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

 

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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());

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.