-2

Kindly show me the error in my code i am trying to connect the sign in form with database using php but i am getting these errors:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\MyProject\php\signin.php on line 9

Notice: Trying to get property of non-object in C:\xampp\htdocs\MyProject\php\signin.php on line 11

Fatal error: Uncaught Error: Call to a member function query() on boolean in C:\xampp\htdocs\MyProject\php\signin.php:17 Stack trace: #0 {main} thrown in C:\xampp\htdocs\MyProject\php\signin.php on line 17

<?php

$servername = "localhost";
$username = "root";
$password="";
$dbname = "myproject";

// Create connection
$conn = mysqli_connect($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO sign_in (username,password)";

if ($conn->query($sql) === TRUE) {
    echo "New record inserted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();

?>
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
Ayaz khan
  • 13
  • 5
  • what is not clear about `Access denied` ? – YvesLeBorg Jan 31 '18 at 16:38
  • Possible duplicate of [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) – Ulrich Thomas Gabor Jan 31 '18 at 16:39

3 Answers3

-1

There must be a password on root mysql account that you're not providing here:

$password="";

Other parts of the error is a direct consequence of this, $conn became a boolean and not an object as the mysqli_connect() is unsuccessful (it gives back false on failure).

Mcload
  • 258
  • 4
  • 17
-1

Your query is not correct, since no values where given to insert. It should look like this:

$sql = "INSERT INTO sign_in (username,password) VALUES ($username,$password)";
Mikelo
  • 256
  • 1
  • 16
  • `VALUES ($username,$password)` - do you know what you wrote here? You do realize that those are not integers, but strings and need to be dealt with correctly. – Funk Forty Niner Jan 31 '18 at 19:15
-1

You are getting this ERROR because of your is mixed up with mysqli_* functions and PDO code.

Methods of both PDO connection and mysqli_* are different.

Here is your error. ******Here is your error*****

$sql = "INSERT INTO sign_in (username,password)";

if ($conn->query($sql) === TRUE) { //ERROR line
    echo "New record inserted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

You have use function mysqli_query() instead of accessing member function of $conn, just take below correct example.

// Perform queries 
mysqli_query($conn,"SELECT * FROM Persons");
mysqli_query($conn,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");

mysqli_close($conn);

Hope you get the point and will help you..

Shishil Patel
  • 2,419
  • 2
  • 10
  • 16
  • Or they could just replace `$conn = mysqli_connect(...)` with `$conn = new mysqli(...)` – CD001 Jan 31 '18 at 16:14
  • *"You are getting this ERROR because of your is mixed up with mysqli_* functions and PDO code. Methods of both PDO connection and mysqli_* are different."* - Huh? Where did *you* see a mix of apis? That should be stricken from the answer. – Funk Forty Niner Jan 31 '18 at 19:14