Home > Net >  An issue being encountered on post displaying system
An issue being encountered on post displaying system

Time:12-20

So, I have this system where you can make a post. So, how it works is, there is an input field, whatever you type in the input field, and click post, it will send to the database as en entry and get posted. The post will be displayed. However, with my current system, after entering something in the input field, and clicking post, the entry gets sent to the database, but the post doesn't actually display. For it to display, you need to refresh the page again, which it displays then, and two entries go to the database.

I don't want this to happen. Right when the user enters text into the input field and clicks post, the post should display on the go, you shouldn't have to refresh for the post to be displayed, and only one entry should be sent to the database, not two. Now, I won't include my database connection and my insert statements, but here is the code to display the post:

<div >
  <?php

  $sql = "SELECT * FROM posts";
  $result = mysqli_query($connection, $sql);
  if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {

  ?>
  <div >
    // all the displayed post content
  </div>
  <?php

  }
}

  ?>
</div>

Insert Statement (post.php):

<?php

session_start();

// Making Connection To The Database

$dbHost = "localhost";
$dbUser = "root";
$dbPass = "root";
$database = "feed";

$connection = mysqli_connect($dbHost, $dbUser, $dbPass, $database) or die ("Sorry, we could not connect to the database");

// Posting System

if (!empty($_POST['postContent'])) {
  $post = $_POST['postContent'];
  $firstname = $_SESSION['firstname'];
  $lastname = $_SESSION['lastname'];

  $sql = "INSERT INTO posts (firstname, lastname, body, date_posted) VALUES (?, ?, ?, NOW())";
  $stmt = mysqli_stmt_init($connection);
  // nested if statement
  if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "";
  } else {
    mysqli_stmt_bind_param($stmt, "sss", $firstname, $lastname, $post);
    mysqli_stmt_execute($stmt);
  }
} else {
  echo "";
}

?>

What should I do to resolve this issue? Please help.

CodePudding user response:

I think it would be better to share the form codes which you submit the data. Anyway, I guess you need to redirect after the script is completed. In PHP you can use header to redirect to new page as bellow:

header("Location: http://www.example.com/same_page.php");
exit()

Write your own PHP page instead of 'same_page.php' Please let me know if you needed more instruction.

CodePudding user response:

Here is an outline for you of the "one php script/self posting" pattern I described briefly in the comment.

The important thing to understand is that if you have a form with no action, the page will submit to itself. You also need to understand the different HTTP request types: 'GET' vs 'POST' specifically.

Last but not least you have to be clear how HTTP Request/response works. When your page is rendered, and it has a form on it, nothing new happens until you create some sort of event through interaction with the page. If you don't understand how HTTP requests work, I would encourage you to learn about them or you will struggle with web development.

<?php
// somepage.php
session_start();
// db includes etc. here.

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['postContent']) {
   // Move all your database update code here.
   // This will only run if it posted.
}
?>
// Include your HTML header that actually starts your html
<!DOCTYPE html>
<form method="post">
  <input id="postContent" name="postContent" etc.>
</form>
<div >
  <?php

  $sql = "SELECT * FROM posts";
  $result = mysqli_query($connection, $sql);
  if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {

  ?>
  <div >
    // all the displayed post content
  </div>
  <?php

  }
}

  ?>
</div>
  •  Tags:  
  • php
  • Related