Page 1 of 1

Registering users (php/mysql)

PostPosted: 27. January 2016 14:08
by sberry
I have a php page for registering users to a database. However, when I try to register a user (for testing), I get messages (1) 'Successfully registered', followed by (2) 'Error while registering you'.

Also, when I retry this(refreshing the page), I get message (1) again.

The register file in a folder called 'actions', which is inside a folder called 'cms'.

This is the PHP code:
Code: Select all
<?php
session_start();
if(isset($_SESSION['user'])!="")
{
 header("Location: ../mainpage.php");
}
include_once '../includes/dbconnect.php';

if(isset($_POST['btn-signup']))
{
 $uname = mysql_real_escape_string($_POST['uname']);
 $email = mysql_real_escape_string($_POST['email']);
 $upass = md5(mysql_real_escape_string($_POST['pass']));
 
 if(mysql_query("INSERT INTO users(username,email,password) VALUES('$uname','$email','$upass')"))
 {
  ?>
        <script>alert('successfully registered ');</script>
        <?php
 }
 else
 {
  ?>
        <script>alert('error while registering you...');</script>
        <?php
 }
}
?>


This is the form html:

<form class="form-signin"method="post">
<h2 class="form-signin-heading text-center">AlphaCMS</h2>
<label class="sr-only">User Name</label>
<input type="text" id="username" class="form-control" placeholder="User Name" required autofocus>
<label class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign Me Up</button>
</form>

'mainpage.php' is where the page should go to next.

I don't know what's wrong so I can't fix it. Any help will be appreciated.

Thanks

Re: Registering users (php/mysql)

PostPosted: 27. January 2016 15:40
by Nobbie
This is a horrible mix of PHP and JavaScript, you cannot mix that in this way. The PHP Script is executed in a completely different environment than JavaScript (i.e. it is executed on different PCs in a different order). You should read some tutorials about PHP and how it is implemented.

PHP runs on the server, where JavaScript is executed by the browser on the clients PC.

Re: Registering users (php/mysql)

PostPosted: 27. January 2016 16:10
by sberry
Thanks, will do just that. I'm new to PHP and JavaScript so was following an example, not a good one I guess.