0

It's just a simple register/login system but i dont know why it doesnt work i'll paste here my config:

<?php 

date_default_timezone_set("Europe/Bucharest");   
$file_name = basename($_SERVER['PHP_SELF'], '.php');    
    
define ('ROOT_PATH', realpath(dirname(__FILE__).'/..'));
//define('BASE_URL()', 'http://localhost/my_book');

function base_url(){
    if(isset($_SERVER['HTTPS'])){
        $protocol = ($_SERVER['HTTPS'] != "off") ? "https" : "http";
    } else{
        $protocol = 'http';
    }
    return $protocol . "://" . $_SERVER['HTTP_HOST'].'/login';
}
   
?>

and my function.php:

<?php
date_default_timezone_set("Europe/Bucharest");
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "uni");
class DB_con
{
    function __construct()
    {
        $con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
        $this->dbh=$con;
        // Check connection
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    }

    // for username availblty
    public function usernameavailblty($uname) {
        $result =mysqli_query($this->dbh,"SELECT Username 
                                        FROM tblusers 
                                        WHERE Username='$uname'");
        return $result;
    }

    // for email availblty
    public function uemailavailblty($email) {
        $result =mysqli_query($this->dbh,"SELECT UserEmail 
                                            FROM tblusers 
                                            WHERE UserEmail='$email'");
        return $result;
    }

    // Function for registration
    public function registration($fname,$uname,$uemail,$pasword)
    {
        $ret=mysqli_query($this->dbh,
                    "insert into tblusers(FullName,Username,UserEmail,Password) 
            values('$fname','$uname','$uemail','$pasword')");
        return $ret;
    }


// Function for signin
public function signin($uname,$pasword)
    {
    $result=mysqli_query($this->dbh,"select id,FullName from tblusers where Username='$uname' and Password='$pasword'");
    return $result;
    }

    function runBaseQuery($query) {
                $result = mysqli_query($this->dbh, $query);
                while($row=mysqli_fetch_assoc($result)) {
                $resultset[] = $row;
                }       
                if(!empty($resultset)){
                return $resultset;
                }
    }
    
    function numRows($query) {
        $result  = mysqli_query($this->dbh, $query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;   
    }
    
    function executeQuery($query) {
        $result  = mysqli_query($this->dbh, $query);
        return $result; 
    }




    




}
?>

the error i get is:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in E:\xampp\htdocs\true\libs\function.php on line 11
Failed to connect to MySQL: Access denied for user 'root'@'localhost' (using password: NO)

and i tried making a new user in phpmyadmin with all the privileges but it still doesnt work

Dancro
  • 1
  • 1
  • 4
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly May 30 '22 at 21:14
  • Set the password for the `root` user account into `define("DB_PASSWORD", "");` – RiggsFolly May 30 '22 at 21:18
  • Your script is open to [SQL Injection Attack](http://stackoverflow.com/questions/60174). Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187) You should always use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's instead of concatenating user provided values into the query. Never trust ANY user input! – RiggsFolly May 30 '22 at 21:18

1 Answers1

-1

This can be caused due to couple of issues

  • once try with default passwords like 1234
  • try to create new user assign all rights, and try to use that user insted of root