fork download
  1. <?php
  2.  
  3. require_once "/lib/pdo.php";
  4.  
  5. $mapper = new DataMapper($DBH);
  6.  
  7. if(isset($_COOKIE['studentscookie']['id'])){
  8. $id = $_COOKIE['studentscookie']['id'];
  9. }
  10. else{
  11. setcookie("studentscookie[id]", $mapper->getLastId()+1, time()+(7*24*60*60*42), "/");
  12. }
  13.  
  14.  
  15.  
  16. $students = $mapper->showAllStudents();
  17.  
  18. if(isset($_POST['submit'])){
  19. $profile=new Profile;
  20. $profile->setFields($_POST);
  21. $profile2=(array)$profile;
  22.  
  23. // foreach ($profile2 as $t):
  24. // echo $t."<br>";
  25. // endforeach;
  26.  
  27. // echo $profile->showGroupnum();
  28. $mapper->addStudent($profile);
  29. }
  30.  
  31.  
  32. // foreach ($students as $student):
  33. // echo $student->showName()."<br>";
  34. // endforeach;
  35.  
  36. include "template.html";
  37.  
  38. ?>
  39.  
  40.  
  41. <?php//pdo
  42.  
  43. require_once "/lib/login.php";
  44. require_once "/lib/Profile.php";
  45.  
  46. $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  47. $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  48.  
  49. class DataMapper
  50. {
  51. public $DBH;
  52.  
  53. public function __construct(PDO $DBH)
  54. {
  55. $this->DBH = $DBH;
  56. }
  57.  
  58. public function showAllStudents()
  59. {
  60.  
  61. $STH = $this->DBH->prepare("SELECT * FROM students");
  62. $STH->execute();
  63. $result = $STH->fetchAll(PDO::FETCH_CLASS, "profile");
  64. return $result;
  65. }
  66.  
  67. public function addStudent(Profile $profile)
  68. {
  69.  
  70. $STH = $this->DBH->prepare("INSERT INTO students (name, surname, sex, groupnum, email, points, year, place)
  71. VALUES (:name, :surname, :sex, :groupnum, :email, :points, :year, :place)");
  72.  
  73. $STH->bindparam(":name", $name);
  74. $STH->bindparam(":surname", $surname);
  75. $STH->bindparam(":sex", $sex);
  76. $STH->bindparam(":groupnum", $groupnum);
  77. $STH->bindparam(":email", $email);
  78. $STH->bindparam(":points", $points);
  79. $STH->bindparam(":year", $year);
  80. $STH->bindparam(":place", $place);
  81.  
  82. $name = $profile->showName();
  83. $surname = $profile->showSurname();
  84. $sex = $profile->showSex();
  85. $groupnum = $profile->showGroupnum();
  86. $email = $profile->showEmail();
  87. $points = $profile->showPoints();
  88. $year = $profile->showYear();
  89. $place = $profile->showPlace();
  90.  
  91. $STH->execute();
  92. }
  93.  
  94. public function editProfile(Profile $profile)
  95. {
  96. $STH = $this->DBH->prepare("UPDATE students SET name=:name, surname=:surname, sex=:sex, groupnum=:groupnum, email=:email, points=:points, year=:year, place=:place WHERE id=:id");
  97. $STH->bindparam(":name", $name);
  98. $STH->bindparam(":surname", $surname);
  99. $STH->bindparam(":sex", $sex);
  100. $STH->bindparam(":groupnum", $groupnum);
  101. $STH->bindparam(":email", $email);
  102. $STH->bindparam(":points", $points);
  103. $STH->bindparam(":year", $year);
  104. $STH->bindparam(":place", $place);
  105. $STH->bindparam(":id", $id);
  106.  
  107. $name = $profile->showName();
  108. $surname = $profile->showSurname();
  109. $sex = $profile->showSex();
  110. $groupnum = $profile->showGroupnum();
  111. $email = $profile->showEmail();
  112. $points = $profile->showPoints();
  113. $year = $profile->showYear();
  114. $place = $profile->showPlace();
  115. $id = $profile->showID();
  116. $STH->execute();
  117. }
  118.  
  119. public function getLastID()
  120. {
  121.  
  122. $STH = $this->DBH->query("SELECT COUNT(*) FROM students");
  123. $result = $STH->fetchColumn();
  124. $id = $result;
  125. return $id;
  126. }
  127. }
  128. ?>
  129.  
  130.  
  131. <?php
  132.  
  133. class Profile{
  134.  
  135. protected $name;
  136. protected $surname;
  137. protected $sex;
  138. protected $groupnum;
  139. protected $email;
  140. protected $points;
  141. protected $year;
  142. protected $place;
  143.  
  144. public function setFields($data)
  145. {
  146. foreach ($data as $key => $value) {
  147. $data[$key] = trim($value);
  148. }
  149.  
  150. $this->name = $data['name'];
  151. $this->surname = $data['surname'];
  152. $this->sex = $data['sex'];
  153. $this->groupnum = $data['groupnum'];
  154. $this->email = $data['email'];
  155. $this->points = $data['points'];
  156. $this->year = $data['year'];
  157. $this->place = $data['place'];
  158. }
  159.  
  160. public function showName()
  161. {
  162. return $this->name;
  163. }
  164.  
  165. public function showSurname()
  166. {
  167. return $this->surname;
  168. }
  169.  
  170. public function showSex()
  171. {
  172. return $this->sex;
  173. }
  174.  
  175. public function showGroupnum()
  176. {
  177. return $this->groupnum;
  178. }
  179.  
  180. public function showEmail()
  181. {
  182. return $this->email;
  183. }
  184.  
  185. public function showPoints()
  186. {
  187. return $this->points;
  188. }
  189.  
  190. public function showYear()
  191. {
  192. return $this->year;
  193. }
  194.  
  195. public function showPlace()
  196. {
  197. return $this->place;
  198. }
  199.  
  200. }
  201. ?>
Runtime error #stdin #stdout #stderr 0.02s 24400KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Warning:  require_once(/lib/pdo.php): failed to open stream: No such file or directory in /home/3OQ7sE/prog.php on line 3
PHP Fatal error:  require_once(): Failed opening required '/lib/pdo.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/3OQ7sE/prog.php on line 3