0

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://i.stack.imgur.com/jVFw8.jpg ). 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>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • What is the error? @Markous – yxlow07 Dec 05 '20 at 07:23
  • 1
    Does this answer your question? [MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)](https://stackoverflow.com/questions/10299148/mysql-error-1045-28000-access-denied-for-user-billlocalhost-using-passw) – Dharman Dec 05 '20 at 13:55

2 Answers2

0

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.

yxlow07
  • 332
  • 2
  • 10
  • it fixed that but I still can't take the infos of my table named Login –  Dec 05 '20 at 09:58
  • I want when I press login (when I write a random username and password) those things to get in my database(named "back") that I have a table Login.So login will have id 1 username (a random things lets say sifajfai ) and a password lets say random 1333.Next time I get in the site will be id 2 username 3r39r and goes on like this –  Dec 05 '20 at 10:09
  • Use $_POST to get the submitted variables and query for the results in the database – yxlow07 Dec 05 '20 at 11:00
  • where exactly should I use that to get it? –  Dec 05 '20 at 16:59
0
  <?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";
    
    ?>
Switi
  • 359
  • 3
  • 6