Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. $conn = new mysqli('localhost', 'ROOT', '', 'chat')
  5. or die ('Cannot connect to db');
  6.  
  7. $result = $conn->query("select userID, userName from user");
  8.  
  9. echo "<html>";
  10. echo "<body>";
  11. echo "<form action='uusiviesti2.php' method='get'>";
  12. echo "<select name='userID'>";
  13.  
  14. while ($row = $result->fetch_assoc()) {
  15.  
  16. unset($id, $name);
  17. $id = $row['userID'];
  18. $name = $row['userName'];
  19. echo "<option value=" . $id . ">" . $name . "</option>";
  20. }
  21.  
  22. echo "</select>";
  23. echo "<input type='submit'/>";
  24. echo "</form>";
  25.  
  26. echo "</body>";
  27. echo "</html>";
  28.  
  29. ?>
  30. -------------------------
  31. <?php
  32.  
  33. $USERID = $_GET["userID"];
  34. $conn=new mysqli('localhost', 'ROOT', '', 'chat')
  35. or die ('Cannot connect to db');
  36.  
  37. $result = $conn->query("SELECT messageID FROM message WHERE messageUSER = '$USERID' AND messageREAD is null");
  38.  
  39. echo "<html>";
  40. echo "<body>";
  41. echo "<form action='uusiviesti3.php' method='get'>";
  42. echo "<select name='messageID'>";
  43.  
  44. while ($row = $result->fetch_assoc()) {
  45.  
  46. unset($id);
  47. $id = $row['messageID'];
  48. echo "<option value=" . $id . ">" . $id . "</option>";
  49. }
  50.  
  51. echo "</select>";
  52.  
  53.  
  54. echo "<table style='border: solid 1px black;'>";
  55. echo "<tr><th>Message</th><th>TO</th></tr>";
  56.  
  57. class TableRows extends RecursiveIteratorIterator {
  58. function __construct($it) {
  59. parent::__construct($it, self::LEAVES_ONLY);
  60. }
  61.  
  62. function current() {
  63. return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
  64. }
  65.  
  66. function beginChildren() {
  67. echo "<tr>";
  68. }
  69.  
  70. function endChildren() {
  71. echo "</tr>" . "\n";
  72. }
  73. }
  74.  
  75. $servername = "localhost";
  76. $username = "ROOT";
  77. $password = "";
  78. $dbname = "chat";
  79.  
  80. try {
  81. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  82. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  83. $stmt = $conn->prepare("SELECT messageID, messageDATA FROM message WHERE messageUSER = '$USERID' AND messageREAD is null");
  84.  
  85. $stmt->execute();
  86.  
  87. // set the resulting array to associative
  88. $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  89. foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
  90. echo $v;
  91. }
  92. }
  93. catch(PDOException $e) {
  94. echo "Error: " . $e->getMessage();
  95. }
  96. $conn = null;
  97. echo "</table>";
  98.  
  99. echo "<input type='submit'/>";
  100. echo "</form>";
  101.  
  102. echo "</body>";
  103. echo "</html>";
  104.  
  105. ?>
  106.  
  107. --------------------
  108.  
  109.  
  110. GNU nano 2.5.3 File: uusiviesti3.php
  111.  
  112. <?php
  113. $messageID = $_GET["messageID"];
  114. $servername = "localhost";
  115. $username = "ROOT";
  116. $password = "";
  117. $dbname = "chat";
  118.  
  119. try {
  120. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  121. // set the PDO error mode to exception
  122. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  123.  
  124. $sql = "UPDATE message SET messageREAD=now() WHERE messageID='$messageID' AND messageREAD is null";
  125.  
  126. // Prepare statement
  127. $stmt = $conn->prepare($sql);
  128.  
  129. // execute the query
  130. $stmt->execute();
  131.  
  132. // echo a message to say the UPDATE succeeded
  133. echo $stmt->rowCount() . " records UPDATED successfully";
  134. }
  135. catch(PDOException $e)
  136. {
  137. echo $sql . "<br>" . $e->getMessage();
  138. }
  139.  
  140. $conn = null;
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement