0

So I use this same method for other pages, and it works just fine. For example, I use this on my login page, after the user fills in the login form, it calls a php file to query the database. If the query fails, it sets a session variable with an error, and redirects you back to the login page. Where on the login page I have something to check if that session variable is set, if it is, then the error message is displayed.

I'm reusing this same way of showing error messages, for user's who try to sign up, if they have already signed up on this page, then my php file that queries the database should redirect them with the error. Now this same method is working just as it did for my login page. However, it is only displayed after a page refresh, which is not ideal.

Here is the php file that holds the html for users to sign-up, and shows the error message if it's set:

<?php
 if(isset($_SESSION['signUpError'])) {
   $error = $_SESSION['signUpError'];
   unset($_SESSION['signUpError']);
   echo '<div class="alert alert-warning alert-dismissible" 
   role="alert" id="logError" style="width:600px; margin-left:auto; margin-right:auto;">
   <span type="button" class="close" data-dismiss="alert" aria-
   label="Close"><span aria-hidden="true">&times;</span></span>
   <strong>' . $error . '</strong></div>';
 }else{
   $error = "";
 }
?>

And the php file that get's called after they sign-up:

<?php
  session_start();
  include("../includes/databaseHandler.inc.php");

  $title = $_POST['title'];
  $newTitle = $_POST['newTitle'];
  $id = $_POST['id'];
  $joinName=  "%".$_POST['joinName']."%";

if($stmt = mysqli_prepare($conn, "SELECT title FROM rooms WHERE title LIKE ?")){
    mysqli_stmt_bind_param($stmt, 's', $joinName);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    if($row = mysqli_fetch_assoc($result)){
    $_SESSION['signUpError'] = "This Dentist/Hygienist is already assigned to a room!";
    header('Refresh:5; url = ../dentist_home.php');
    exit;
  }
}

if($stmt = mysqli_prepare($conn, "UPDATE rooms SET title = ? WHERE title = ? AND id = ?" )){
  mysqli_stmt_bind_param($stmt, "sss", $newTitle, $title, $id);
  mysqli_stmt_execute($stmt);
  $result = mysqli_stmt_get_result($stmt);
}

mysqli_close($conn);
?>

For the login page, I use header("Location: file_name.php"); but if I use that on this page it doesn't show the error at all, even if the page is refreshed. I'm still kind of new to PHP, so any insight into what I may be doing wrong would be great.

Frank
  • 99
  • 13
  • because; the session was only set after the session was started – Funk Forty Niner Apr 16 '17 at 22:30
  • 1
    Do you mean by this, that i didn't call session_start() for the page where the error message is displayed. If that's what you're thinking, I did call session start, but it's in a seperate php block at the top of that page, if that makes a difference. – Frank Apr 16 '17 at 22:34

1 Answers1

0

So, what you're saying is:

  1. Someone goes to the file that contains your second PHP block.
  2. There's already a matching row in the db.
  3. Your script sets an error message in the session and redirects to the login page.
  4. The login page is supposed to show the error but is not until the page is refreshed.

Is that right?

Phl3tch
  • 155
  • 3
  • 12
  • Yes, an ajax request above in the code, not shown, passes the information along to the file called changeRoomName.php which is the second php block you see. when it goes out to the database it sees if the name of the person signing up is already there, if it is it sets the $_SESSION['signUpError'] message and redirects them back to the page where they tried to sign up. And that error message gets displayed in the file dentist_home.php which is the code on the top block posted – Frank Apr 17 '17 at 00:13
  • Could be a million things. There's a thread here with a lot of possibles: https://stackoverflow.com/questions/17242346/php-session-lost-after-redirect – Phl3tch Apr 17 '17 at 00:41