Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. <?php
  2. $server = 'SQL2008.net.dcs.hull.ac.uk';
  3. $connectionInfo = array("Database" => "rde_504507");
  4. $conn = sqlsrv_connect($server, $connectionInfo);
  5. date_default_timezone_set('Europe/London');
  6.  
  7. $date = new DateTime();
  8. $dateString = $date->format('d-m-Y H:i');
  9. $num = 1;
  10. $test = "test";
  11.  
  12. if ($_POST) {
  13. $desiredName = $_POST['name'];
  14. $desiredLocation = $_POST['location'];
  15. $locationQuery = "SELECT StudentNo, Location, UpdateTime FROM location WHERE StudentNo='$desiredName'";
  16. $results = sqlsrv_query($conn, $locationQuery, array($desiredName));
  17.  
  18.  
  19. if (sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) { //already present in the DB
  20. updateArchive('$num', '$desiredName', '$test', '$dateString');
  21. $row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC);
  22. $location = $row['Location'];
  23. $updateQuery = "UPDATE location SET Location = '$desiredLocation', UpdateTime = '$dateString' WHERE StudentNo = '$desiredName'";
  24. sqlsrv_query($conn, $updateQuery);
  25. echo $desiredName, " location changed to ", $desiredLocation;
  26. } else { //not yet added
  27. $insertQuery = "INSERT INTO location (StudentNo, Location, UpdateTime) VALUES ('$desiredName', '$desiredLocation', '$dateString')";
  28. sqlsrv_query($conn, $insertQuery);
  29. echo $desiredName, " added to the database at ", $desiredLocation;
  30.  
  31. }
  32.  
  33. }
  34.  
  35.  
  36. else {
  37. $desiredName = $_GET['name'];
  38. $locationQuery = "SELECT StudentNo, Location, UpdateTime FROM location WHERE StudentNo='$desiredName'";
  39.  
  40. $results = sqlsrv_query($conn, $locationQuery, array($desiredName));
  41.  
  42. if ($results) {
  43. $row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC);
  44. echo $desiredName;
  45. echo " is at ";
  46. echo $row['Location'];
  47. echo " at ";
  48. echo $row['UpdateTime'];
  49. } else {
  50. echo "No user found";
  51. }
  52.  
  53. }
  54.  
  55. sqlsrv_close($conn);
  56.  
  57.  
  58. function updateArchive($num, $desiredName, $test, $dateString)
  59. {
  60. $archiveQuery = "INSERT INTO history (ID, StudentNo, Location, UpdateTime) VALUES ('$num', '$desiredName', '$test', '$dateString')";
  61. }
  62.  
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement