skip to Main Content

I have created a login form and I am trying with php to connect on my database(localhost-PHPMyAdmin). I have created a database name "back".I created a table name it login and I have 4 things inside it,those things on the link(https://imgur.com/u9C1r1h ).
I give the part of codes, one for the php code that I have a problem with, and the second code of the form.
Furtheremore, "if(gethostname()==’the site I log in and that it is fine‘" there is a link here that I don’t want to give it ,but the site is right,similar with this " $mysqli = new mysqli($host, $user, $pass, $db,null,’here I have my socket and it is right ‘);" my socket is fine.Where is the problem can’t connect those two things?How to make those connected?

<?php
$host='localhost';
$db = 'back';
require_once "info.php";

$user=$DB_USER;
$pass=$DB_PASS;


if(gethostname()=='the site I log in and that it is fine') {
    $mysqli = new mysqli($host, $user, $pass, $db,null,'here I have my socket and it is right ');
} else {
        $mysqli = new mysqli($host, $user, $pass, $db); //here it shows me an error Failed to connect to MySQL: (1045) Access denied for user 'root'@'localhost' (using password: YES)
}

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . 
    $mysqli->connect_errno . ") " . $mysqli->connect_error;
}


?>







<form method="post" action="index.php">
Username: <input name="username"/>
<br/>
Password: <input name="pass" type="password"/>
<br/>
<input type="submit" value="LOGIN"/>
<input name="p" type="hidden">
</form>

2

Answers


  1. The issue you are encountering happens most probably was caused as you did not create a user with that username/password. If you have created the user, check your credentials. If not just put an empty string for the password. Normally, root users for localhost will not have a password assigned to it.

    Get the post variables

    <?php
    if (isset($_POST['LOGIN']) && $_SERVER['REQUEST_METHOD'] === "POST"){
         $username = $_POST['username'];
        //The rest do it yourself
    }
    

    Get the result from database

    <?php
    $prepare = $mysqli->prepare("SELECT * FROM back WHERE username=? AND password=?");
    $prepare->bind_param("ss", $_POST['username'], $_POST['password']);
    if ($prepare->execute()) {
     echo "User found";
    } else {
     echo "No user found";
    }
    

    Please note that what I am showing above is not secure as I did not password_verify() it. But what I showed was merely going to give you an idea.

    Login or Signup to reply.
  2.   <?php
      // Database configuration 
        $host = "localhost"; // hostname
        $user = "";//add your user name
        $pass = "";//add your password
        $db   = "back"; // databasename
        
        //// Create connection
        $conn = mysqli_connect($host, $user, $pass, $db);
        
        // Check connection
        if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
        }
        echo "Connected successfully";
        
        ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search