0

I've started writing a registration script, and within the script I've created a variable name $message. The variable is supposed to update it's string value based off of different conditions. The first instance of the variable has no condition, and in that case it echos properly on the registration page that is included at the bottom of the registration script. All other instances of the $message variable fail to produce text on the screen when echoed. I'm new to php, so I'm sure it's something simple, though I can't figure it out for some reason. I thought maybe scope is the problem, but I think that only applies to functions and not if statements. I don't know. Anyways, here is the script:

<?php
try{
$dbc = new PDO('mysql:host=localhost;dbname=logreg', 'root', '');
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbc->exec('SET NAMES "utf8"');
} catch (Exception $ex)
{
    $error = 'Couldn\'t connect to the databse';
    include 'includes/error.html.php';
    exit();
}

$sql = 'SELECT name, email, password FROM users';
$result = $dbc->query($sql);

while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
    $names[] = $row['name'];
    $email[] = $row['email'];
}

$message = 'Fill out to sign up.';

if (isset($_POST['name'],$_POST['email'], $_POST['password']) and $_POST['name'] and $_POST['email'] and $_POST['password'] !== '')
{
   if (in_array($_POST['name'], $names)){
       $message = 'That user already exists';
       exit();
   }
       if (in_array($_POST['email'], $email)) {
            $message = 'That email already has an account registered';
            exit();
        }

    $sql = $dbc->prepare('INSERT INTO users SET
        name = ?,
        email = ?,
        password = ?');
    $name = strtolower($_POST['name']);
    $email = strtolower($_POST['email']);
    $password = $_POST['password'];
    $result = $sql->execute(array(
            $name,
            $email,
            md5($password . 'saltnpepper')
                )
            );
            exit();
} 
include 'register.php';

Then as well, here is the code for the register page where the echo of $message takes place, and the form exists.

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php echo $message; ?>
        <form action="" method="post">
            <input type="text" name="name" placeholder="Username">
            <input type="text" name="email" placeholder="email">
            <input type="password" name="password" placeholder="password">
            <input type="submit" value="register">
        </form>
    </body>
</html>

Any help is greatly appreciated!



5
  • 2
    Perhaps exit(); is the problem Commented Jan 18, 2015 at 23:30
  • 1
    you use exit(); in a wrong way. just comment all that lines with exit firstly, and check the logic of your if-else statements and behavior Commented Jan 18, 2015 at 23:31
  • 1
    After setting $message, you're using exit() which will terminate the script. As you want to echo that variable, exit() is causing troubles there. Try commenting those lines and try again. Commented Jan 18, 2015 at 23:40
  • /*I've tried removing the exits, but that didn't change the problem of the $message variables not working in the echo statement.*/ Commented Jan 18, 2015 at 23:46
  • This is actually correct, and I left one exit statement in by accident on the bottom, causing me to make the comment above this one. Just removing the exits made the $message variable have multiple values though, when I only wanted it to have one based on events, so this wasn't the complete solution, but darn close, and thank you very much for helping! Commented Jan 19, 2015 at 0:24

2 Answers 2

1

Your issue, as stated in the comments by Mark is your use of exit(). Taken from the manual, it says the following:

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

Read More

Which means that every time you have exit(), it stops your script right then and there. Meaning it will never print out your $message variable. You want to remove those exit()'s and your script will work.


Also, you're going about the registration process the wrong way. You're over complicating it. The most widely used way of doing it is to run a query and check if rows are returned using a where clause with the data provided. Example:

$sql = $dbc->prepare('SELECT email,name FROM users WHERE email = ? OR name = ?');
$name = strtolower($_POST['name']);
$email = strtolower($_POST['email']);
$result = $sql->execute(array($name, $email));
$count = $sql->rowCount();

if ($count > 0) {
    // user exists
} else {
    // user doesn't exist
    // add them to db
}

The above is just food for thought :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I kind of figured I was nuking the process a little bit lol
Pleasure @RandyC, it's always the simple issues that cause the biggest headaches!
0

Sorry, have no possibility to debug with your real data, but just replace your code right after:

if (isset($_POST['name'],$_POST['email'], $_POST['password']) and $_POST['name'] and $_POST['email'] and $_POST['password'] !== '')

with this fragment :

{
   if (in_array($_POST['name'], $names)){
       $message = 'That user already exists';
   } else {
       if (in_array($_POST['email'], $email)) {
            $message = 'That email already has an account registered';
        } else {

        $sql = $dbc->prepare('INSERT INTO users SET
        name = ?,
        email = ?,
        password = ?');
    $name = strtolower($_POST['name']);
    $email = strtolower($_POST['email']);
    $password = $_POST['password'];
    $result = $sql->execute(array(
            $name,
            $email,
            md5($password . 'saltnpepper')
                )
            );

       }
   }
} 

3 Comments

This does work, but can you give me the logic behind what I was doing didn't work as expected for my learning and future avoidance of my error please?
I wrote as many others, you was using exit() command wrong way. so when you set up $message variable and exit after - you never get to the last line with your include 'register.php';
Yep, I realize this now. I thought I had removed all of the exits, but left one at the very bottom thinking it was still needed. I took it off and all was well. Thanks again. Your solution is the one that worked the most logically, because simply removing the exits made the message variable hold and print two values, instead of just one value as intended, so that is why I marked your solution as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.