-4

When I was trying to make website for school project then I make a registration page so I make a html page and a php and a database. But when I tried to enter anything in form then the result after submitting the information is blank page. When I opened php file in browser then a blank page opened. There should be either connected to the database or failed to connect to database. My coding of php file is given below: Please help me as less time is remaining for last date of submission.

  <?php

define('DB_HOST', 'localhost');
define('DB_NAME', 'practice');
define('DB_USER', 'root');
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()
{
    $fullname = $_POST['name'];
    $userName = $_POST['user'];
    $email    = $_POST['email'];
    $password = $_POST['pass'];
    $query    = "INSERT INTO websiteusers(fullname,userName,email,pass) VALUES    ('$fullname','$userName','$email','$password')";
    $data = mysql_query($query) or die(mysql_error());
    if ($data) {
        echo "YOUR REGISTRATION IS COMPLETED...";
    }
}

function SignUp()
{
    if (!empty($_POST['user'])) //checking the 'user' name which is from  Sign-Up.html, is it empty or have some text
        {

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

        if (!$row = mysql_fetch_array($query) or die(mysql_error())) {
            newuser();
        } else {
            echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
        }
    }
}
if (isset($_POST['submit'])) {
    SignUp();
}
?>
saulotoledo
  • 1,737
  • 3
  • 19
  • 36
  • 2
    what errors are you getting and why are you coding for something that isn't of this century? not to mention being completely unsafe mind you. Edit: *Don't be shy to upvote this* ;-) – Funk Forty Niner Nov 20 '17 at 14:12
  • `mysql_fetch_array($query) or die(mysql_error())` that doesn't work with this, but the query itself. – Funk Forty Niner Nov 20 '17 at 14:13
  • Have you checked your PHP logs for errors? Do you ever invoke those functions that you define? If you don't actually call those functions then what you have is code which produces no output. So receiving no output seems reasonable... – David Nov 20 '17 at 14:13
  • You gotta love the missing parts. – Funk Forty Niner Nov 20 '17 at 14:14
  • You shouldn't use mysql_* since it's deprecated. use mysqli_* or PDO. – Ofir Baruch Nov 20 '17 at 14:14
  • Well I see that you miss-spelled your function name `newuser()` which should be `NewUser()` but I agree with @Fred-ii- your code is unsafe and not practical. The tutorial you're following [link](http://mrbool.com/how-to-create-a-sign-up-form-registration-with-php-and-mysql/28675) is a rather old one. Try looking up a recent tutorial. – Niels Nov 20 '17 at 14:16
  • **Never store plain text passwords!** Please use ***PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html)*** to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). ***It is not necessary to [escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Nov 20 '17 at 14:17
  • 1
    @Niels function names aren't usually case-sensitive :-) thanks for being with me on this one ;-) – Funk Forty Niner Nov 20 '17 at 14:17
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Nov 20 '17 at 14:19
  • use mysqli to connect to the database. stop using mysql –  Nov 20 '17 at 14:20
  • your question is not clear – Onic Team Nov 20 '17 at 14:21
  • Are you opening this as a file, or from a web server? – Jay Blanchard Nov 20 '17 at 14:23
  • @Niels That link you left isn't safe though. Better to use mysqli/pdo with a prepared statement and `password_hash()/password_verify()` ;-) Edit: oops... I misread lol yeah, rather old. – Funk Forty Niner Nov 20 '17 at 14:23
  • @Niels I know, that's why I edited my comment ;-) you'll need to reload it. – Funk Forty Niner Nov 20 '17 at 14:25
  • If anyone have full correct code please send it me as I am not a expert in php it is my first time I use php I got this code from a website when searched on google because I am working for a school project. I am in class 10 so I donot have greater knowledge about it. Please help me. – Rao Aayush Yadav Nov 20 '17 at 14:32

2 Answers2

0

MySQL is depreciated. Use MySQli instead

Connect to your database this way

$connect = mysqli_query(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

then check this way

if($connect){
echo "Connected";
}else{
echo "Not connected";
}

and any query you run should be in this format

$query = mysqli_query($connect,$Sql);
UniQue
  • 96
  • 1
  • 9
0

I tried many source codes from google but I am unsuccessful everytime. Everytime I clicked on submit button it shows a blank page. Please provide me help I want to create a signup page and in this I want to connect html coding to mysql database I have knowledge about html. So please provide an easy way to interconnect the database and html. I also came to know about the php so I tried to copy source code from google but everytime I failed as php preview in browser is blank page.