我正在使用 php、一些 javascript 和 sql server 2008 进行调度项目。用户可以使用作业表单来更新已调度的作业。可以更改的字段有:作业编号、开始时间、结束时间、描述和作业状态。sql 需要在更新之前测试所有这些东西。
- 我遇到的一些问题是:
- 如果用户增加了结束时间(到以后的时间),它允许在调度中重叠
- 如果用户将开始时间移回(更早的时间),则允许重叠
- 如果用户从过去选择一个日程安排日期,它会与未来安排的项目发生日程安排冲突。
我已经坚持了两天,并且已经朝着多个方向解决了这些问题。这是我目前拥有的:
<?php
include_once("../php/functions.php");
$conn = connect();
$params = array
(
$jobNum = $_POST['jobNum'],
$asset = $_POST['drp_asset'],
$jobStatus = $_POST['drp_status'],
$sDate = $_POST['startTime'],
$eDate = $_POST['endTime'],
$department = $_POST['drp_department'],
$descrip = $_POST['txtarea_description'],
$job = $_POST['jobid'],
$asset2 = $_POST['drp_asset']
);
/********************************************
Check to see if the delete button was pressed
And if the pre-check warning was confirmed
delete the record.
********************************************/
if (isset($_POST['updateDelete']))
{
$tsql = "Delete from Calendar_view
where JobID = '$job'";
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt)
{
checkRows($stmt);
returnTo($conn);
}
else
{
errMsg();
}
}
/****************************************
If the times and/or asset have not changed
update the job status
****************************************/
else
{
$tsql_check ="select StartTime, EndTime, Asset
from Calendar_View
where '$sDate' = StartTime and '$eDate' = EndTime and '$asset' = Asset";
$stmt = sqlsrv_query($conn, $tsql_check);
$rows = sqlsrv_has_rows($stmt);
if($stmt)
{
if ($rows === true)
{
updateJobStatus($conn, $params);
}
else
{
timeChanges($conn, $params);
}
}
}
function checkOverlaps($conn, $params)
{
$sDate = $params[3];
//$sDate = new DateTime($params[4]);
//$sDate = date_format($sDate,'Y-m-d G:i:s');
$eDate = $params[4];
//$eDate = new DateTime($params[5]);
//$eDate = date_format($eDate,'Y-m-d G:i:s');
$asset = $params[1];
$tsql_check ="select StartTime, EndTime
from Calendar_View
where (('$sDate' < EndTime and '$sDate' >= StartTime) or ('$eDate' < EndTime and '$eDate' > StartTime)) and '$asset' = Asset";
$stmt = sqlsrv_query($conn, $tsql_check);
if ($stmt)
{
// If there is a match, there will be an overlap
$rows = sqlsrv_has_rows($stmt);
if ($rows === true)
{
checkRows($stmt);
}
//If there is no match then job is being moved
//to an open spot
else
{
updateJob($conn, $params);
}
}
}
/************************************
If the start time or end time have changed
/***********************************/
function timeChanges($conn, $params)
{
$sDate = $params[3];
$eDate = $params[4];
$asset = $params[1];
$tsql_timeCheck ="select StartTime, EndTime
from Calendar_View
where (('$sDate' <= StartTime) or ('$eDate' <= EndTime)) and '$asset' = Asset";
$stmt2 = sqlsrv_query($conn, $tsql_timeCheck);
if ($stmt2 == true)
{
$rows = sqlsrv_has_rows($stmt2);
if ($rows === true)
{
updateJobStatus($conn, $params);
//updateJobStatus($conn, $params);
}
else
{
checkOverlaps($conn, $params);
}
}
}
function updateJob($conn, $params)
{
$tsql = "UPDATE Calendar_View
SET JobNum = ?,
Asset = ?,
JobStatus =?,
StartTime =?,
EndTime =?,
Department = ?,
Description = ?
WHERE JobID = ?";
$stmt = sqlsrv_query($conn, $tsql, $params);
if ($stmt)
{
checkRows($stmt);
returnTo($conn);
}
else
{
errMsg();
}
}
/***************************************
Update job status
****************************************/
function updateJobStatus($conn, $params)
{
$tsql = "UPDATE Calendar_View
SET JobNum = ?,
Asset = ?,
JobStatus =?,
StartTime =?,
EndTime =?,
Department = ?,
Description = ?
WHERE JobID = ? and Asset = ?";
$stmt = sqlsrv_query($conn, $tsql, $params);
if ($stmt)
{
checkRows($stmt);
returnTo($conn);
}
else
{
errMsg();
}
}
/**********************************
Return user to scheduling page they
were working on.
***********************************/
function returnTo($conn)
{
sqlsrv_close($conn);
echo "<meta http-equiv='refresh' content='1;URL=../pages/schedule2.php' />";
}
function errMsg()
{
die( print_r(sqlsrv_errors(), true));
exit;
}
/*************************************
Check if any rows were altered by the
query. If so alert success, else alert
that there was a conflict (nothing done)
**************************************/
function checkRows($stmt)
{
$rows_affected = sqlsrv_rows_affected($stmt);
if ($rows_affected > 0)
{
echo "Job updated successfully!<br>";
}
else
echo "There was a scheduling conflict.";
echo "<meta http-equiv='refresh' content='2;URL=../pages/schedule2.php' />";
}
?>
任何和所有的帮助将不胜感激!