Empty fields message don't show properly












2















When user trying to edit his blog post and left some fields empty, he should get empty fields error, but, code that should do that get ignored and i get completely different error message instead.



My question: how to display empty fields error message properly?



Code snippets from editpost.php file:



1. Checking for empty fields:



// Check for empty fields
if (empty($title) || empty($body)) {
// Save correct data into fields
header('Location: editpost.php?error=emptyeditpostfield&title='.$title.'&body='.$body);
// Stop script
exit();
} else {
$query = "UPDATE posts SET title='$title', body='$body' WHERE id = {$update_id}";
}


2. Desired message:



<!-- Check if user has rights to editing post -->
<?php if (isset($_SESSION['id'])) : ?>
<?php if ($_SESSION['name'] == $post['author']) : ?>
<div class="container">
<h1>Edit Post</h1>
<?php
// Message I want to show
if (isset($_GET['error'])) {
if ($_GET['error'] == 'emptyeditpostfield') {
echo '<p class="text-warning">Fill in all fields!</p>';
}
}
?>
<!-- Edit post form... -->


3. Message I receive:



<!-- /Edit post form... -->
</div>
<?php else : header('Location: index.php?error=accessdenied'); exit(); // Message I got ?>
<?php endif; ?>
<?php else : header('Location: index.php?error=accessdenied'); exit(); ?>
<?php endif; ?>


Edited (Nov 30).



4. Code that is a part of this issue as well:



    // Get ID
$id = mysqli_real_escape_string($conn, $_GET['id']);

// Create Query
$query = "SELECT * FROM posts WHERE id = $id";

// Get Result
$result = mysqli_query($conn, $query);

// Fetch Data
$post = mysqli_fetch_assoc($result);
// var_dump($posts);

// Free Result
mysqli_free_result($result);

// Close Connection
mysqli_close($conn);


Picture 1. Warnings after Submit with empty fields.



Warnings



Picture 2. Edit post page.



Edit Post Page










share|improve this question

























  • This part equals false : $_SESSION['name'] == $post['author']

    – Thomas Lefetz
    Nov 19 '18 at 10:41











  • "i get completely different error message instead" - but you are not going to tell us what it is?

    – Mawg
    Nov 19 '18 at 13:12
















2















When user trying to edit his blog post and left some fields empty, he should get empty fields error, but, code that should do that get ignored and i get completely different error message instead.



My question: how to display empty fields error message properly?



Code snippets from editpost.php file:



1. Checking for empty fields:



// Check for empty fields
if (empty($title) || empty($body)) {
// Save correct data into fields
header('Location: editpost.php?error=emptyeditpostfield&title='.$title.'&body='.$body);
// Stop script
exit();
} else {
$query = "UPDATE posts SET title='$title', body='$body' WHERE id = {$update_id}";
}


2. Desired message:



<!-- Check if user has rights to editing post -->
<?php if (isset($_SESSION['id'])) : ?>
<?php if ($_SESSION['name'] == $post['author']) : ?>
<div class="container">
<h1>Edit Post</h1>
<?php
// Message I want to show
if (isset($_GET['error'])) {
if ($_GET['error'] == 'emptyeditpostfield') {
echo '<p class="text-warning">Fill in all fields!</p>';
}
}
?>
<!-- Edit post form... -->


3. Message I receive:



<!-- /Edit post form... -->
</div>
<?php else : header('Location: index.php?error=accessdenied'); exit(); // Message I got ?>
<?php endif; ?>
<?php else : header('Location: index.php?error=accessdenied'); exit(); ?>
<?php endif; ?>


Edited (Nov 30).



4. Code that is a part of this issue as well:



    // Get ID
$id = mysqli_real_escape_string($conn, $_GET['id']);

// Create Query
$query = "SELECT * FROM posts WHERE id = $id";

// Get Result
$result = mysqli_query($conn, $query);

// Fetch Data
$post = mysqli_fetch_assoc($result);
// var_dump($posts);

// Free Result
mysqli_free_result($result);

// Close Connection
mysqli_close($conn);


Picture 1. Warnings after Submit with empty fields.



Warnings



Picture 2. Edit post page.



Edit Post Page










share|improve this question

























  • This part equals false : $_SESSION['name'] == $post['author']

    – Thomas Lefetz
    Nov 19 '18 at 10:41











  • "i get completely different error message instead" - but you are not going to tell us what it is?

    – Mawg
    Nov 19 '18 at 13:12














2












2








2








When user trying to edit his blog post and left some fields empty, he should get empty fields error, but, code that should do that get ignored and i get completely different error message instead.



My question: how to display empty fields error message properly?



Code snippets from editpost.php file:



1. Checking for empty fields:



// Check for empty fields
if (empty($title) || empty($body)) {
// Save correct data into fields
header('Location: editpost.php?error=emptyeditpostfield&title='.$title.'&body='.$body);
// Stop script
exit();
} else {
$query = "UPDATE posts SET title='$title', body='$body' WHERE id = {$update_id}";
}


2. Desired message:



<!-- Check if user has rights to editing post -->
<?php if (isset($_SESSION['id'])) : ?>
<?php if ($_SESSION['name'] == $post['author']) : ?>
<div class="container">
<h1>Edit Post</h1>
<?php
// Message I want to show
if (isset($_GET['error'])) {
if ($_GET['error'] == 'emptyeditpostfield') {
echo '<p class="text-warning">Fill in all fields!</p>';
}
}
?>
<!-- Edit post form... -->


3. Message I receive:



<!-- /Edit post form... -->
</div>
<?php else : header('Location: index.php?error=accessdenied'); exit(); // Message I got ?>
<?php endif; ?>
<?php else : header('Location: index.php?error=accessdenied'); exit(); ?>
<?php endif; ?>


Edited (Nov 30).



4. Code that is a part of this issue as well:



    // Get ID
$id = mysqli_real_escape_string($conn, $_GET['id']);

// Create Query
$query = "SELECT * FROM posts WHERE id = $id";

// Get Result
$result = mysqli_query($conn, $query);

// Fetch Data
$post = mysqli_fetch_assoc($result);
// var_dump($posts);

// Free Result
mysqli_free_result($result);

// Close Connection
mysqli_close($conn);


Picture 1. Warnings after Submit with empty fields.



Warnings



Picture 2. Edit post page.



Edit Post Page










share|improve this question
















When user trying to edit his blog post and left some fields empty, he should get empty fields error, but, code that should do that get ignored and i get completely different error message instead.



My question: how to display empty fields error message properly?



Code snippets from editpost.php file:



1. Checking for empty fields:



// Check for empty fields
if (empty($title) || empty($body)) {
// Save correct data into fields
header('Location: editpost.php?error=emptyeditpostfield&title='.$title.'&body='.$body);
// Stop script
exit();
} else {
$query = "UPDATE posts SET title='$title', body='$body' WHERE id = {$update_id}";
}


2. Desired message:



<!-- Check if user has rights to editing post -->
<?php if (isset($_SESSION['id'])) : ?>
<?php if ($_SESSION['name'] == $post['author']) : ?>
<div class="container">
<h1>Edit Post</h1>
<?php
// Message I want to show
if (isset($_GET['error'])) {
if ($_GET['error'] == 'emptyeditpostfield') {
echo '<p class="text-warning">Fill in all fields!</p>';
}
}
?>
<!-- Edit post form... -->


3. Message I receive:



<!-- /Edit post form... -->
</div>
<?php else : header('Location: index.php?error=accessdenied'); exit(); // Message I got ?>
<?php endif; ?>
<?php else : header('Location: index.php?error=accessdenied'); exit(); ?>
<?php endif; ?>


Edited (Nov 30).



4. Code that is a part of this issue as well:



    // Get ID
$id = mysqli_real_escape_string($conn, $_GET['id']);

// Create Query
$query = "SELECT * FROM posts WHERE id = $id";

// Get Result
$result = mysqli_query($conn, $query);

// Fetch Data
$post = mysqli_fetch_assoc($result);
// var_dump($posts);

// Free Result
mysqli_free_result($result);

// Close Connection
mysqli_close($conn);


Picture 1. Warnings after Submit with empty fields.



Warnings



Picture 2. Edit post page.



Edit Post Page







php session






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 '18 at 20:22







BlackB0ne

















asked Nov 19 '18 at 10:29









BlackB0neBlackB0ne

486




486













  • This part equals false : $_SESSION['name'] == $post['author']

    – Thomas Lefetz
    Nov 19 '18 at 10:41











  • "i get completely different error message instead" - but you are not going to tell us what it is?

    – Mawg
    Nov 19 '18 at 13:12



















  • This part equals false : $_SESSION['name'] == $post['author']

    – Thomas Lefetz
    Nov 19 '18 at 10:41











  • "i get completely different error message instead" - but you are not going to tell us what it is?

    – Mawg
    Nov 19 '18 at 13:12

















This part equals false : $_SESSION['name'] == $post['author']

– Thomas Lefetz
Nov 19 '18 at 10:41





This part equals false : $_SESSION['name'] == $post['author']

– Thomas Lefetz
Nov 19 '18 at 10:41













"i get completely different error message instead" - but you are not going to tell us what it is?

– Mawg
Nov 19 '18 at 13:12





"i get completely different error message instead" - but you are not going to tell us what it is?

– Mawg
Nov 19 '18 at 13:12












2 Answers
2






active

oldest

votes


















0














You're redirecting the user when the fields are not filled. You do not save the post therefore the $post is not filled with the data of your form. $post['author'] does not exist.






share|improve this answer































    0














    To solve this issue:



    1. In first code snippet need store post id in session as follow:



    // Store post id in session
    session_start();
    $_SESSION['update_id'] = $update_id;


    2. In fourth code snippet need check if session has id as follow:



    // Get ID
    if (isset($_SESSION['update_id'])) {
    $id = $_SESSION['update_id'];
    } else {
    $id = $_GET['id'];
    }


    Now post id is stored in session and it's possible to retrieve data from database again after redirection.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372626%2fempty-fields-message-dont-show-properly%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You're redirecting the user when the fields are not filled. You do not save the post therefore the $post is not filled with the data of your form. $post['author'] does not exist.






      share|improve this answer




























        0














        You're redirecting the user when the fields are not filled. You do not save the post therefore the $post is not filled with the data of your form. $post['author'] does not exist.






        share|improve this answer


























          0












          0








          0







          You're redirecting the user when the fields are not filled. You do not save the post therefore the $post is not filled with the data of your form. $post['author'] does not exist.






          share|improve this answer













          You're redirecting the user when the fields are not filled. You do not save the post therefore the $post is not filled with the data of your form. $post['author'] does not exist.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 10:42









          Thomas LefetzThomas Lefetz

          1215




          1215

























              0














              To solve this issue:



              1. In first code snippet need store post id in session as follow:



              // Store post id in session
              session_start();
              $_SESSION['update_id'] = $update_id;


              2. In fourth code snippet need check if session has id as follow:



              // Get ID
              if (isset($_SESSION['update_id'])) {
              $id = $_SESSION['update_id'];
              } else {
              $id = $_GET['id'];
              }


              Now post id is stored in session and it's possible to retrieve data from database again after redirection.






              share|improve this answer






























                0














                To solve this issue:



                1. In first code snippet need store post id in session as follow:



                // Store post id in session
                session_start();
                $_SESSION['update_id'] = $update_id;


                2. In fourth code snippet need check if session has id as follow:



                // Get ID
                if (isset($_SESSION['update_id'])) {
                $id = $_SESSION['update_id'];
                } else {
                $id = $_GET['id'];
                }


                Now post id is stored in session and it's possible to retrieve data from database again after redirection.






                share|improve this answer




























                  0












                  0








                  0







                  To solve this issue:



                  1. In first code snippet need store post id in session as follow:



                  // Store post id in session
                  session_start();
                  $_SESSION['update_id'] = $update_id;


                  2. In fourth code snippet need check if session has id as follow:



                  // Get ID
                  if (isset($_SESSION['update_id'])) {
                  $id = $_SESSION['update_id'];
                  } else {
                  $id = $_GET['id'];
                  }


                  Now post id is stored in session and it's possible to retrieve data from database again after redirection.






                  share|improve this answer















                  To solve this issue:



                  1. In first code snippet need store post id in session as follow:



                  // Store post id in session
                  session_start();
                  $_SESSION['update_id'] = $update_id;


                  2. In fourth code snippet need check if session has id as follow:



                  // Get ID
                  if (isset($_SESSION['update_id'])) {
                  $id = $_SESSION['update_id'];
                  } else {
                  $id = $_GET['id'];
                  }


                  Now post id is stored in session and it's possible to retrieve data from database again after redirection.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 11 '18 at 14:45

























                  answered Dec 11 '18 at 14:40









                  BlackB0neBlackB0ne

                  486




                  486






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372626%2fempty-fields-message-dont-show-properly%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)