Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Db info
- $servername = "localhost";
- $username = "tasty";
- $password = "";
- $dbname = "tasty";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- echo 'Connection failed!';
- }
- $food = basename($_SERVER['PHP_SELF']);
- $food = str_replace(".php", "", $food);
- $fetchcom = "SELECT * FROM " . $food . "";
- $result = $conn->query($fetchcom) or trigger_error($conn->error);
- echo "<div class='b-comments'>
- <h1>Comments</h1>";
- while($row = $result->fetch_array())
- {
- 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> ";
- echo "<br />";
- }
- ?>
- <div id="b-comments">
- <?php
- $divStyle='display:block;'; // show div
- if(!isset($_SESSION['login_user'])){
- $divStyle='style="display:none;"'; //hide div
- echo "Please log in to comment";
- }
- print'<div '.$divStyle.'><div class="com"><form action="" method="post">
- <label>Comment</label><br><br>
- <input id="com" name="Comment" placeholder="Comment" type="text"><br>
- <input id="com" name="com" type="submit" value=" Submit ">
- <span>
- <?php
- echo $error;
- ?>
- </span>
- </form></div><div>';
- // Check if button is pressed
- if(isset($_POST['submit'])) {
- // Check if any comment is written
- if(empty($_POST['com'])) {
- $noCom = 'Please dont send empty comments!!!';
- }
- // Button pressed and comment is not empty
- else {
- //Db info
- $servername = "localhost";
- $username = "tasty";
- $password = "";
- $dbname = "tasty";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // Proceed if connection is established
- else {
- $comment = $_POST['com'];
- $food = str_replace(".php", "", basename($_SERVER['PHP_SELF']));
- echo $food;
- $name = $_SESSION['login_user'];
- echo $name;
- // Prepare to post comment to database
- $sql = $conn->query("INSERT INTO " . $food . "(comname, comtext) VALUE ('$name', '$comment')");
- $conn->query($sql) or die();
- //header($food . ".php");
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement