所以我有这个程序使用javascript / php应该是一种时间卡系统,签入,将签入日期发送到数据库,签出,它比较日期然后它计算你的数量&# 39;制作完成后,将其发送回数据库。
现在的问题是,javascript / ajax触发的php脚本似乎每个会话只运行一次,因为它运行代码,然后重新运行一个快照(?)代码,它不再与数据库一起更新,除非我进入新的会话。
以下是我使用timeSystem.php的代码:(目前只使用测试按钮,其他按钮/代码仍然在那里)
<?php
session_start();
?>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
function timeCard(id) {
if(id == "Test"){
$(function(){
$.get("insert.php", function(data){
alert(data);
});
});
}
else if(id == "Test2"){
$(function(){
$.get("singin.php", function(data){
alert(data);
});
});
}
}
</script>
<?php
if(!$_SESSION['driverid']){
header("location:login.php");
}
else {
?>
</head>
<body>
<h1 style="float: left" class="center">Unity Transport Authority SMS (Shift Management System)</h1> <h3>V0.2</h3>
<div style="clear: both"></div>
<table>
<tr>
<td><button type="button" id="Test" onclick="timeCard(id)">Test</button></td>
<td><button type="button" id="Test2" onclick="timeCard(id)">Test2</button></td>
<td><form action="logout.php" mothod="post"><input type="submit"></form> </td>
</tr>
</table>
</body>
</html>
<?php
}
?>
insert.php:
<?php
session_start();
$con=mysqli_connect(/* Removing info but this is working as intended */);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$t = time();
$Driverno = $_SESSION['id'];
$DBDriver= mysqli_query($con,"SELECT * FROM AdminForm
WHERE DRIVERNO=$Driverno");
while ($row = mysqli_fetch_array($DBDriver)) {
if ($row['CURSTATE'] == 0) {
$sql="UPDATE AdminForm SET SINGING=$t, CURSTATE =1
WHERE DRIVERNO=$Driverno";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Have a nice shift!";
} else {
echo "You are already signed in!";
}
}
mysqli_close($con);
?>
singin.php:
<?php
session_start();
$con=mysqli_connect(/* Again just removed because personal info */);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$Driverno = $_SESSION['id'];
$Payrate = 1510;
/* Select the driver from the database */
$DBDriver= mysqli_query($con,"SELECT * FROM AdminForm
WHERE DRIVERNO=$Driverno");
while ($row = mysqli_fetch_array($DBDriver)) {
if ($row['CURSTATE'] == 1) { /* Checks if user is signed in */
if ($row['RANK'] == 1) {
$t = time();
$DBtime=$row['SINGING'];
echo $row['SINGING'] . "<br>";
$time = $t - $DBtime; /* Compare the time to the database */
echo "Total Amount of time Worked This Session (in seconds): " . $time . "<br>";
$Totalhours = $time + $row['TOTALHRS'];
$Totalhours = $Totalhours / 60 / 60; /* Conversion from seconds into hourly format */
echo "Total Hours Worked: " . $Totalhours . "hrs";
echo "<br>";
$Totalpayout = $Totalhours /*/ 60 / 60*/ * $Payrate;
echo "Total Payout is now: " . $Totalpayout . "$";
$Paid = 0;
$sql="UPDATE AdminForm SET TOTALHRS=$Totalhours, TOTALPAYOUT=$Totalpayout, PAID=$Paid, CURSTATE=0
WHERE DRIVERNO=$Driverno";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
else/* else if? */ {
// Future code for different ranks
echo "This is bad !";
die();
}
}
else {
echo "Please sign in before attemping to sign out!";
}
};
mysqli_close($con);
?>
发生的一个例子是,你登录然后按下test2按钮,如果你在值班,它会检查CURSTATE。如果没有,它会说&#34;有一个很好的转变&#34;,并且更改CURSTATE,然后如果你再次按下按钮,它将不会再次运行代码并检查你是否值班,它将只是说&#34;有一个很好的转变&#34;试。
但是如果您注销并重新登录,会重置会话,它会再次运行代码,并会说“#34;您已经登录了!&#34;。
我真的希望你得到我正在谈论的内容并且可以帮助我,并且感谢您提前提供任何帮助!