Guestbook for interviews


/ Published in: PHP
Save to your folder(s)

Guestbook for interviews


Copy this code and paste it in your HTML
  1. define ('EPP',3);
  2.  
  3. check_base();
  4.  
  5. if (isset($_POST['submit']))
  6. save_vals();
  7.  
  8. show_entries();
  9.  
  10.  
  11. show_form();
  12.  
  13. function check_base() {
  14. global $db;
  15. error_reporting(E_ERROR);
  16. $db = new mysqli('','','','');
  17. error_reporting(E_ALL);
  18. if (mysqli_connect_errno())
  19. throw new Exception ('connect error: '.mysqli_connect_error());
  20.  
  21. $rez = $db->query ("show tables like 'gbookz'");
  22.  
  23. if ($rez->num_rows==0) {
  24. $db->query('create table gbookz (id int(11) not null auto_increment,'.
  25. 'username varchar(20), message varchar(200), pubdate datetime, '.
  26. ' primary key(`id`))');
  27.  
  28. if ($db->errno!=0) {
  29. echo $db->error;
  30. exit();
  31. }
  32.  
  33.  
  34. echo 'created gbookz table<br/>';
  35. }
  36. $rez->free();
  37.  
  38. }
  39.  
  40. function save_vals() {
  41.  
  42. global $db;
  43.  
  44. $stmt = $db->prepare('insert into gbookz(username,message,pubdate) '.
  45. 'values(?,?,?)');
  46.  
  47. $stmt->bind_param('sss', $_POST['username'], $_POST['message'],
  48. date('Y-m-d H:i:s'));
  49.  
  50. $stmt->execute();
  51.  
  52. if ($stmt->errno!=0) {
  53. throw new Exception ('db error: '.$stmt->error);
  54. }
  55.  
  56. echo 'record saved with id='.$stmt->insert_id.'<br/>';
  57. }
  58.  
  59. function show_entries() {
  60.  
  61. global $db;
  62.  
  63. $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
  64.  
  65. $rez = $db->query('select count(*) from gbookz');
  66.  
  67. $num_rows = $rez->fetch_array(MYSQLI_NUM);
  68. $pages = ceil($num_rows[0]/EPP);
  69.  
  70. $rez->free();
  71.  
  72. ob_start();
  73. for ($i=0; $i<$pages; $i++) {
  74. echo str_repeat('&nbsp',3);
  75. if ($i+1==$page)
  76. echo ($i+1);
  77. else
  78. echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($i+1).'">'.($i+1).'</a>';
  79.  
  80. if (($i+1)%10==0)
  81. echo '<br/>';
  82. }
  83. $nav = ob_get_clean();
  84.  
  85.  
  86. $stmt = $db->prepare('select id,username,message,pubdate from gbookz order by pubdate desc '.
  87. 'limit ?,?');
  88.  
  89. $start = ($page-1)*EPP;
  90. $num = EPP;
  91. $stmt->bind_param('dd', $start, $num);
  92.  
  93. $stmt->execute();
  94. $stmt->bind_result($id, $username, $message, $pubdate);
  95. while ($stmt->fetch()) {
  96.  
  97. echo '<h2>'.$username.'</h2>';
  98. echo '<p>'.$message.'</p>';
  99. echo '<p>'.date('d.m.Y H:i',strtotime($pubdate)).'</p>';
  100. }
  101.  
  102. $stmt->close();
  103. echo $nav;
  104.  
  105. }
  106.  
  107. function show_form() {
  108. ?>
  109.  
  110. <form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post"
  111. enctype="application/www-form-urlencoded">
  112.  
  113. <p>input username <input type="text" name="username"></p>
  114. input message<br/> <textarea name="message"></textarea>
  115. <br/><input type="submit" name="submit" value="send" />
  116. </form>
  117.  
  118. <?php
  119. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.