0

I followed a lot of tutorial on google/youtube but still unable to connect database using php. I coded exactly according to documentation for database connection but it doesn,t work.I have already created database named as userregistration using mysql dashboard. Here is myindex.html file:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Login and Registration</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css"   href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="login-box">
    <div class="row">
        <div class="col-md-6 login-left">
            <h2>Login Here</h2>
            <form action="validation.php" method="post">
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="user" class="form-control" required>
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" required>
                </div>
                <button type="submit" class="btn btn-primary">Login</button>

            </form>

        </div>
        <div class="col-md-6 login-right">
            <h2>Register Here</h2>
            <form action="registration.php" method="post">
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="user" class="form-control" required>
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" required>
                </div>
                <button type="submit" class="btn btn-primary">Register</button>

            </form>

        </div>
    </div>
    </div>
</div>
</body>
</html>

Here is my Registration.php file:

<?php

session_start();

$con = mysqli_connect('localhost','root','');
mysqli_select_db($con, 'userregistration');
$name = $_POST['user'];
$pass = $_POST['password'];

$s = "select * from  usertable where name='$name'";
$result = mysqli_query($con, $s);
$num = mysqli_num_rows($result);

if($num == 1){
    echo "Username Already Taken";
}else{
    $reg = "insert into usertable(name, password) values ('$name','$pass')";
    mysqli_query($con, $reg);
    echo "Registration Successful";
}
?>

Is there any error in my code?

7
  • 3
    Didn't work is not a very helpful error description, what message the website gave when you submit your registration form? Commented Sep 30, 2019 at 6:31
  • error shows(404 Not Found) Commented Sep 30, 2019 at 6:33
  • how do you structure your project? action="registration.php" say it will look for the file registration.php in the same folder of the HTML file Commented Sep 30, 2019 at 6:37
  • If you are on a *nix platform, file names are case sensitive as you have registration.php in your code and the file name you show as Registration.php (although this may be just in the question). Commented Sep 30, 2019 at 6:38
  • @NigelRen yeah this name is just in question. I,m using registration.php in small case in my project Commented Sep 30, 2019 at 6:42

2 Answers 2

1

Try using directly the connection with db name

based on patter

$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

try

$con = mysqli_connect('localhost','root','','userregistration');

and be sure your php page is in the correct path
eventually try adding a proper path or at least a relative path

   <form action="./Registration.php" method="post">
Sign up to request clarification or add additional context in comments.

6 Comments

are you sure that username is localhost,, the password is root and the db name is userregistration check better ..anyway error 404 is not a db error but a page not found http error check for you link (and path) to the php page
You have missed the password out of the connect parameters.
no the username is not localhost. Servername is localhost, username is root, password is empy and database name is userregistration.
then adgiust the proper value in the right sequence answer updated ..
I also applied your updated code but again unfortunately it shows the same error when i click to register button(the error is 404 not found)
|
0

Your PHP part looks alright. Maybe you can try to see if the PHP is able to make the connection by adding such a debugging code like how it is documented here, https://www.php.net/manual/en/function.mysqli-connect.php

if (!$con) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($con) . PHP_EOL;

Finally, MOST IMPORTANTLY, you should probably not be using this directly because this can introduce serious SQL-Injection vulnerabilities. Google a little more about the best practices. :)

Comments

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.