Search code examples
phpregistration

PHP/SQL - Blank page for registered users for a simple registration page


I have an assignment where I have to make a registration page in php.... I just want to keep things simple so making the form work is all I'm aiming for. I am aware of the vulnerability of sql injections/plaintext, but that's the last of my worries for now since it's a class assignment.

The script below works as far as inserting new users/passwords, but if there's an existing user, the page is blank and doesn't give a warning. I'm looking for help in giving the error "Sorry, this user already exists" shown on the screen (or something).

Thanks :D.

<?php

define('DB_HOST', 'localhost');
define('DB_NAME', '////////');
define('DB_USER','/////////');
define('DB_PASSWORD','///////////');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

function NewUser() { $userName = $_POST['user']; $password = $_POST['pass']; $query = "INSERT INTO UserName (userName,pass) VALUES ('$userName','$password')"; $data = mysql_query ($query)or die(mysql_error()); if($data) { echo "YOUR REGISTRATION IS COMPLETED..."; } } function SignUp() { if(!empty($_POST['user']))  { 

$query = mysql_query("SELECT * FROM UserName WHERE userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); 

if(!$row = mysql_fetch_array($query))) 
{ newuser(); } 
else { echo "SORRY...YOU ARE ALREADY REGISTERED USER..."; } } } if(isset($_POST['submit'])) { SignUp(); } ?>

Solution

  • First, Its really important check your php_error_log or Add error reporting into the TOP of your file.

    <?php 
       error_reporting(E_ALL);
       ini_set('display_errors', 1);
    

    There is an extra closing parenthesis and you are calling an undefined function.
    Assuming these are the errors, fix then with:

    if(!$row = mysql_fetch_array($query)) { 
        NewUser();
    } 
    else {
         echo "SORRY...YOU ARE ALREADY REGISTERED USER..."; 
    }
    

    Hope it helps you.