Reputation: 21
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?
Upvotes: 0
Views: 105
Reputation: 133360
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">
Upvotes: 1
Reputation: 26
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. :)
Upvotes: 0