更新新记录时恢复以前更新的记录 MySQL PHP

发布于 2024-12-10 06:07:57 字数 2394 浏览 0 评论 0原文

祝大家美好的一天。我正在尝试为我的系统制作一个任务跟踪模块。逻辑是,每当我将任务分配给用户时,系统都会将该任务更新为“IsTaken”,这意味着特定用户负责该任务。更新后,它会在“user_task”表中创建一个条目,该条目基本上将任务和用户表联系在一起。每当我将任务分配给某人时都很好。但是,当我再次执行此操作时,前一个任务记录的 IsTaken 字段将恢复为 0。我尝试重新分配它,但前一个记录又恢复为 0。这很奇怪。我正在使用 XAMPP、MySQL 和 PHP。我希望我不是唯一经历过这种事的人。任何帮助将不胜感激。

这是我的表格:

CREATE TABLE IF NOT EXISTS `task` (
  `Task_No` int(11) NOT NULL AUTO_INCREMENT,
  `Task_Name` varchar(100) NOT NULL,
  `Task_Desc` varchar(450) DEFAULT NULL,
  `Task_DateCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `Task_IsTaken` tinyint(1) NOT NULL,
  PRIMARY KEY (`Task_No`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `user_task` (
  `UT_No` int(11) NOT NULL AUTO_INCREMENT,
  `User_Email` varchar(100) NOT NULL,
  `Task_No` int(11) NOT NULL,
  `Task_Duration` varchar(20) NOT NULL,
  `Task_DateTaken` date DEFAULT NULL,
  `Task_DateFinished` timestamp NULL DEFAULT NULL,
  `Task_IsIssue` tinyint(1) NOT NULL,
  PRIMARY KEY (`UT_No`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

这是我的脚本(来自表单):

//Process - Insert Task
if(isset($_POST['btnCreateTask']))
{
if($_POST['taskName']!=NULL)
{
$taskName = mysql_real_escape_string ($_POST['taskName']);
$taskDesc = mysql_real_escape_string ($_POST['taskDesc']);
$insertTask = "INSERT INTO task(Task_Name, Task_Desc, Task_IsTaken) VALUES('$taskName', '$taskDesc', 0)";
$sqlResult1 = mysql_query($insertTask);}
else
{
    echo "No task name given";
    $errorCode = 1;
}
}


if(isset($_POST['btnAssignTask']))
{
    if($_POST['assignTaskName']!=NULL)
{
    $assigntaskName = mysql_real_escape_string($_POST['assignTaskName']);
    $assigntaskNo = mysql_real_escape_string($_POST['assignTaskNo']);
    $assigntaskOwner = mysql_real_escape_string($_POST['assignTaskOwner']);
    $assigntaskDuration = mysql_real_escape_string($_POST['assignTaskDuration']);
    $updateUpcomingTask = "UPDATE task SET Task_IsTaken = '1' AND Task_No = '$assigntaskNo'";
    $createUserTask = "INSERT INTO user_task (User_Email, Task_No, Task_Duration, Task_DateTaken, Task_DateFinished) 
                        VALUES ('$assigntaskOwner', '$assigntaskNo', '$assigntaskDuration', '$now', NULL)";
    $sqlResult2 = mysql_query($updateUpcomingTask);
    $sqlResult3 = mysql_query($createUserTask);
}
else
{
    echo "No task selected";
    $errorCode = 2;
}
}

Good day to all. I'm trying to make a task tracking module for my system. The logic is, whenever I assign a task to a user, the system updates the task to "IsTaken" meaning the particular user is responsible for that task. Upon updating, it creates an entry into "user_task" table which basically ties together the task and the user table. Whenever I assign a task to somebody it's fine. But when I do it again, the previous task record's IsTaken field reverts back to 0. I try to re-assign it, but again, the previous record reverts. It's quite weird. I'm using XAMPP, MySQL and PHP. I'm hoping I'm not the only one experiencing this. Any help would be much appreciated.

Here are my tables:

CREATE TABLE IF NOT EXISTS `task` (
  `Task_No` int(11) NOT NULL AUTO_INCREMENT,
  `Task_Name` varchar(100) NOT NULL,
  `Task_Desc` varchar(450) DEFAULT NULL,
  `Task_DateCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `Task_IsTaken` tinyint(1) NOT NULL,
  PRIMARY KEY (`Task_No`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `user_task` (
  `UT_No` int(11) NOT NULL AUTO_INCREMENT,
  `User_Email` varchar(100) NOT NULL,
  `Task_No` int(11) NOT NULL,
  `Task_Duration` varchar(20) NOT NULL,
  `Task_DateTaken` date DEFAULT NULL,
  `Task_DateFinished` timestamp NULL DEFAULT NULL,
  `Task_IsIssue` tinyint(1) NOT NULL,
  PRIMARY KEY (`UT_No`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

And here are my scripts (from the form):

//Process - Insert Task
if(isset($_POST['btnCreateTask']))
{
if($_POST['taskName']!=NULL)
{
$taskName = mysql_real_escape_string ($_POST['taskName']);
$taskDesc = mysql_real_escape_string ($_POST['taskDesc']);
$insertTask = "INSERT INTO task(Task_Name, Task_Desc, Task_IsTaken) VALUES('$taskName', '$taskDesc', 0)";
$sqlResult1 = mysql_query($insertTask);}
else
{
    echo "No task name given";
    $errorCode = 1;
}
}


if(isset($_POST['btnAssignTask']))
{
    if($_POST['assignTaskName']!=NULL)
{
    $assigntaskName = mysql_real_escape_string($_POST['assignTaskName']);
    $assigntaskNo = mysql_real_escape_string($_POST['assignTaskNo']);
    $assigntaskOwner = mysql_real_escape_string($_POST['assignTaskOwner']);
    $assigntaskDuration = mysql_real_escape_string($_POST['assignTaskDuration']);
    $updateUpcomingTask = "UPDATE task SET Task_IsTaken = '1' AND Task_No = '$assigntaskNo'";
    $createUserTask = "INSERT INTO user_task (User_Email, Task_No, Task_Duration, Task_DateTaken, Task_DateFinished) 
                        VALUES ('$assigntaskOwner', '$assigntaskNo', '$assigntaskDuration', '$now', NULL)";
    $sqlResult2 = mysql_query($updateUpcomingTask);
    $sqlResult3 = mysql_query($createUserTask);
}
else
{
    echo "No task selected";
    $errorCode = 2;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2024-12-17 06:07:57

您的代码应更改为:

if(isset($_POST['btnAssignTask']))
{
    if($_POST['assignTaskName']!=NULL)
{
    $assigntaskName = mysql_real_escape_string($_POST['assignTaskName']);
    $assigntaskNo = intval($_POST['assignTaskNo']);
    $assigntaskOwner = mysql_real_escape_string($_POST['assignTaskOwner']);
    $assigntaskDuration = mysql_real_escape_string($_POST['assignTaskDuration']);
    $updateUpcomingTask = "UPDATE task SET Task_IsTaken = '1' WHERE Task_No = $assigntaskNo";
    $createUserTask = "INSERT INTO user_task (User_Email, Task_No, Task_Duration, Task_DateTaken, Task_DateFinished) 
                    VALUES ('$assigntaskOwner', '$assigntaskNo', '$assigntaskDuration', '$now', NULL)";
    $sqlResult2 = mysql_query($updateUpcomingTask);
    $sqlResult3 = mysql_query($createUserTask);
}
else
{
    echo "No task selected";
    $errorCode = 2;
}
}

Your code should be changed to:

if(isset($_POST['btnAssignTask']))
{
    if($_POST['assignTaskName']!=NULL)
{
    $assigntaskName = mysql_real_escape_string($_POST['assignTaskName']);
    $assigntaskNo = intval($_POST['assignTaskNo']);
    $assigntaskOwner = mysql_real_escape_string($_POST['assignTaskOwner']);
    $assigntaskDuration = mysql_real_escape_string($_POST['assignTaskDuration']);
    $updateUpcomingTask = "UPDATE task SET Task_IsTaken = '1' WHERE Task_No = $assigntaskNo";
    $createUserTask = "INSERT INTO user_task (User_Email, Task_No, Task_Duration, Task_DateTaken, Task_DateFinished) 
                    VALUES ('$assigntaskOwner', '$assigntaskNo', '$assigntaskDuration', '$now', NULL)";
    $sqlResult2 = mysql_query($updateUpcomingTask);
    $sqlResult3 = mysql_query($createUserTask);
}
else
{
    echo "No task selected";
    $errorCode = 2;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文