-4

My HTML on my page is eliminated after running my PHP validation on the input form.

HTML: InsertUser.php

<?php
session_start();
require_once('File Below'); 
echo $errors; //Errors is local variable within insertBackend.php i know. i just wanted this example to be exact compared next to my code.
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script       
</head>
<body>
    <div class="container">
        <form class="form-horizontal" role="form" method="post" action="InsertUser.php">
            <div class="form-group">
                <label for="txtName" class="col-sm-2 control-label">Name:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="inputName" name="txtName" placeholder="Name" value="">
                </div>
            </div>
            <div class="form-group">
                <label for="txtPassword" class="col-sm-2 control-label">Password:</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" id="inputPassword" name="txtPassword" placeholder="Password" value="">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-10 col-sm-offset-2">
                    <input id="btnSubmit" name="insert_form" type="submit" value="Submit" class="btn btn-primary">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-10 col-sm-offset-2">
                    <input id="btnCreate" onclick="location.href = 'createUser.php';" name="createUser" type="button" value="Create User" class="btn btn-primary">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-10 col-sm-offset-2">
                    <input id="btnsame" onclick="location.href = 'InsertUser.php';" name="InsertUser" type="button" value="Insert User" class="btn btn-primary">
                </div>
            </div>
        </form>
    </div>

</body>
</html>

PHP Page: InsertBackend.php

<?php
session_start();
require_once('*DIRECTORY CONTAINING SQL CONNECTION*'); //works fine
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}

if(isset($_POST['insert_form']))
{
$_SESSION['InsertUser'] = "Insert User Pass"; //This ensures i make it INTO the method
$name= "";
$password = "";
$nameInsert = mysqil_real_escape_string($_POST['txtName']);
//^This right here. mysqli_real_escape_string();
$passInsert = mysqil_real_escape_string($_POST['txtPassword']);
//^This right here. mysqli_real_escape_string();
$errors = array();
if(empty($nameInsert))
{
    array_push($errors, "Please enter your Name");
}
if(empty($passInsert))
{
    array_push($errors, "Please enter your password");
}
if(count($errors = 0))
{
    $name = mysqil_real_escape_string($con, $_POST['txtName']);
    $password = mysqil_real_escape_string($con, $_POST['txtPassword']);
    $hashPass = password_hash($password, PASSWORD_DEFAULT);

    $sqlInsert = "INSERT INTO Table (Name, Phash)
    VALUES ('$name', '$hashPass')";
    $_SESSION['VerifyHash'] = $hashPass; // Super security issue using a hashed password as a flag? I know. Just wanted visual representation of pass to compare next to my database

    if ($con->query($sqlInsert) === TRUE) 
    {
        header('location: InsertUser.php');
        echo "New record created successfully <br>";
    } 
    else 
    {
        header('location: InsertUser.php');
        echo "Error: " . $sql . "<br>" . $con->error;
    }

}
}
echo "Connected"; //Flag to verify Database connection throughout usage. If it makes it here then its connected. End flag.

//CLOSE DATABASE CONNECTION
mysqli_close($con);

?> 

When I run all of this together and enter test data. It arrives on: InsertUser.php. and directs properly. However it displays nothing. No code, no Sessions, no HTML. Nothing? However when I simply refresh the page without navigating anywhere it displays the entire insert form fine. And 2 of my flags display:

ConnectedInsert User Pass

I then proceed to eliminate session data. Then I just see

Connected 

above the login form. After this all takes place there is no change in my Database? And no users are actually added.

Using this information I can deduce that:

  • my form submits to my InsertBackend.php file when called from the form.

  • is connected to my database appropriately.

And that my Backend script is:

  • Not correctly inserting into the database.

  • Not properly hashing my password input.

  • Not rendering the HTML when called back to the insert form.

I have tried really hard to figure out where exactly in this chain of events things are going awry. However I have been unable to figure out why it is not all calling properly, and why my inserts are not working at all.

I really tried finding something on here that would help me figure it out. Unfortunately I was unable to locate anything that gave me clarity. And after the last few hours i have decided to see if anyone here might have any helpful insight into my issue. Just to even have a second set of eyes on it.

halfer
  • 18,701
  • 13
  • 79
  • 158
Morjee
  • 75
  • 8
  • do not `mysqli_real_escape` passwords, since you hash them later anyway. – Jeff Jan 27 '19 at 18:03
  • 2
    This is a bit of a dizzying walk-through of a variety of problems you're observing. Can you narrow it down to just one? One specific operation you're performing which produces an unexpected result? Once you have each individual operation working, then connecting them into a series of operations becomes a lot more straightforward for you. – David Jan 27 '19 at 18:04
  • where and how is `InsertBackend.php` connected at all? – Jeff Jan 27 '19 at 18:06
  • in `InsertUser.php` you don't start the session, so `echo $_SESSION['test'];` will fail. – Jeff Jan 27 '19 at 18:08
  • after adding session_start(); in my InsertUser.php the result is the same. No HTML is displayed after calling the form – Morjee Jan 27 '19 at 18:13
  • @Morjee: Have you checked the PHP error logs? A completely blank page usually indicates an error. – David Jan 27 '19 at 18:15
  • @David : Would using: error_get_last() at the top of InsertUser.php achieve that? – Morjee Jan 27 '19 at 18:26
  • @Morjee: Checking the PHP error logs would achieve that. Also take a look at error reporting options: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – David Jan 27 '19 at 18:28
  • 2
    `if(count($errors = 0))` change to `if(count($errors) == 0)` for a start .... – Matthew Page Jan 27 '19 at 18:31
  • @David Cool! and also "print_r(error_get_last());" at the aforementioned location will provide me with extensive information. Just in case anyone else reading this down the road runs into this exact same specific scenario regarding my question. Thank you David! – Morjee Jan 27 '19 at 18:39
  • @David I just found the error logs. I'm an idiot – Morjee Jan 29 '19 at 03:20

1 Answers1

0

This was just poorly built with no session checking. The session cannot be created if it already exists i suppose.

Using

print_r(error_get_last());

Provides me with

Array ( [type] => 8 [message] => session_start(): A session had already been started - ignoring [file] => *InsertBackend.php* [line] => 2 )

Maybe? I guess i have to test this theory. I will update code as i progress until page is all displaying and inserting into database in case anyone else runs into a similar issue down the road.

Morjee
  • 75
  • 8