summaryrefslogtreecommitdiffstats
path: root/Website/insertData.php
blob: 2facf1c69056421c48252973833fab926171d59c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?

function getNewTaskNo()
{
 $sql = "SELECT MAX(taskNo) FROM TaskTable"; //the SQL command to find the max value in taskNo
 $execute = mysql_query($sql); //execute the command
 $result = mysql_result($execute,0);

 if ($result == NULL)
 {
    // there is no single job in the table
    return 1;
 }
 else
 { 
    //increment the new taskNo
    return $result + 1;
 } 
}


function insertTask($taskNo, $from, $to)
{
 $trans = "START TRANSACTION;";
 $execute = mysql_query($trans);
 
 if ($execute!=1)
 {
    return 3; //could not start transaction
 }
 
 $sql = "INSERT INTO `TaskTable` (`taskNo`, `from`, `to`) VALUES($taskNo, '$from', '$to')";
 $execute = mysql_query($sql); //insert new task into the TaskTable

 if ($execute == 1) //I added to the DB the task!
 {
    $sql = "SELECT @taskID := LAST_INSERT_ID()"; //find the last taskId
    $execute = mysql_query($sql);
    $taskId = mysql_result($execute,0); //I have the last taskId which I will use to insert it into TempTaskTable

    $sql = "INSERT INTO `TempTaskTable` (`taskID`, `taskNo`, `from`, `to`) VALUES($taskId, $taskNo, '$from', '$to')";
    $execute = mysql_query($sql); //insert new task into the TempTaskTable

    if ($execute == 1)
    {  
       $trans = "COMMIT;"; //command to end the transaction
       $execute = mysql_query($trans);//finish the transaction

       if ($execute == 1)
       {
          return 1; //task was successfuly added to both tables, TaskTable and TempTaskTable
       }
       else
       {
          return 0; //nothing was added to the tables
       }
    }
    else
    {
       return 2; //the task is in the TaskTable but not in TempTaskTable
    }
 }
 else
 {
    return 0; //nothing was added to the table, some error occured
 }
}

?>