Advertisement
Guest User

Untitled

a guest
Sep 14th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php
  2.  
  3.     //Db info
  4.     $servername = "localhost";
  5.     $username = "tasty";
  6.     $password = "";
  7.     $dbname = "tasty";
  8.  
  9. // Create connection
  10. $conn = new mysqli($servername, $username, $password, $dbname);
  11.  
  12. // Check connection
  13. if ($conn->connect_error) {
  14.     die("Connection failed: " . $conn->connect_error);
  15.     echo 'Connection failed!';
  16. }
  17.     $food = basename($_SERVER['PHP_SELF']);
  18.     $food = str_replace(".php", "", $food);
  19.     $fetchcom = "SELECT * FROM " . $food . "";
  20.     $result = $conn->query($fetchcom) or trigger_error($conn->error);
  21.     echo "<div class='b-comments'>
  22.                 <h1>Comments</h1>";
  23. while($row = $result->fetch_array())
  24.   {
  25.   echo "<div class='com'><div class='name'><h3>" . $row['comname'] . "</h3></div><br><div class='date'><h4> " . $row['comdate'] . "</h4></div><div class='textcom'> " . $row['comtext'] . "</div></div> ";
  26.   echo "<br />";
  27.   }
  28. ?>
  29. <div id="b-comments">
  30. <?php
  31. $divStyle='display:block;'; // show div
  32.  
  33. if(!isset($_SESSION['login_user'])){
  34.   $divStyle='style="display:none;"'; //hide div
  35.   echo "Please log in to comment";
  36. }
  37.  
  38. print'<div '.$divStyle.'><div class="com"><form action="" method="post">
  39. <label>Comment</label><br><br>
  40. <input id="com" name="Comment" placeholder="Comment" type="text"><br>
  41. <input id="com" name="com" type="submit" value=" Submit ">
  42. <span>
  43. <?php
  44. echo $error;
  45. ?> 
  46. </span>
  47. </form></div><div>';
  48. // Check if button is pressed
  49. if(isset($_POST['submit'])) {
  50.    
  51.     // Check if any comment is written
  52.     if(empty($_POST['com'])) {
  53.         $noCom = 'Please dont send empty comments!!!';
  54.     }
  55.    
  56.     // Button pressed and comment is not empty
  57.     else {
  58.    
  59.         //Db info
  60.         $servername = "localhost";
  61.         $username = "tasty";
  62.         $password = "";
  63.         $dbname = "tasty";
  64.  
  65.         // Create connection
  66.         $conn = new mysqli($servername, $username, $password, $dbname);
  67.        
  68.         // Check connection
  69.         if($conn->connect_error) {
  70.             die("Connection failed: " . $conn->connect_error);
  71.         }
  72.        
  73.         // Proceed if connection is established
  74.         else {
  75.  
  76.             $comment = $_POST['com'];      
  77.             $food = str_replace(".php", "", basename($_SERVER['PHP_SELF']));
  78.             echo $food;
  79.             $name = $_SESSION['login_user'];
  80.             echo $name;
  81.             // Prepare to post comment to database
  82.             $sql = $conn->query("INSERT INTO " . $food . "(comname, comtext) VALUE ('$name', '$comment')");
  83.             $conn->query($sql) or die();
  84.                 //header($food . ".php");
  85.         }
  86.     }      
  87. }
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement