1

I'm trying to display a message upon a successful user registration however what I have doesn't seem to be working and just submits/refreshes the page. No message pops up even though data has successful been entered into the SQL database. Any tips or ideas?

<?php
    require('php/connect.php');
    if (isset($_POST['username']) && isset($_POST['password'])){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $query = "INSERT INTO `user` (username, password, email) VALUES ('$username', '$password', '$email')";
        $result = mysql_query($query);
        if($result){
           echo $msg = "Successful Registration!";
        }
    }
?>

Learning PHP currently so sorry if there's a really obvious answer here!

Edit: Forgot to include echo. Just needed a second pair of eyes, sorry guys. Thanks for the tips!

1
  • you're not echoing anything. Use echo $msg Commented May 31, 2014 at 6:11

3 Answers 3

2

First of all have this on consideration:

If you're getting started on PHP, please stop using mysql. It's deprecated, instead, you can use either PDO or MySQLi

As for your issue, your message is not being printed. Please make sure you echo the $msg variable:

 $msg = "Successful Registration!";
 echo $msg;

In mysqli a standard connection would be:

$DBConnect = new mysqli('serverName', 'userName', 'userPassword', 'dbName');

And that's it, that's all you need to start querying your database using mysqli.

For a mysql connection, try debugging it, see if there's a connection:

$connection = mysql_connect('localhost', 'userName', 'userPassword');
if (!$connection) {
   die('Could not connect: ' . mysql_error());
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the tip re: PDO. My school is (stupidly) teaching this though at the moment.
Maybe just for the basics, and if they stay with this, I guess you have no choice, just make sure if you'll be a PHP programmer, not to use mysql. mysqli (which I use and strongly encourage you to) has an OO interface that is very easy to use. I'll show you how to create a connection, I'll edit my post
1

Try to echo message

if($result)
{
    echo $msg = "Successful Registration!";
}

Comments

0
<?php
echo "Successfully registered!";
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.