1

I'm Posting data using PHP into a mysqlserver. (Rest service) The problem is I'm getting this error:

Connection failed: Access denied for user 'root'@'localhost' (using password: YES).

I changed my query from an insert to a select statement and it works, i could retrieve the data but i cant seem to do an insert. I have checked privileges and even changed password. I'm Sure this question has been asked before but the solutions i have tried don't work problem could be unique to context. I'm not sure if its the code or what? Please help.

<?php

$servername = "localhost";
$username = "root";
$password = "****";
$dbname = "TripBuddy";

$name = $_POST['Name'];
$surname = $_POST['Surname'];
$email = $_POST['Email'];
$password = $_POST['Password'];

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

 $sql = "INSERT INTO 'TripBuddy'.'AppUser01' ('Name', 'Surname', 'Email',              'Password') VALUES ('$name','$surname','$email','$password');";

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

  $conn->close();
  ?>
  • 2
    I'm willing to bet that, it's because you're using the same variable for both `$password`. Change one of them. I'll bet it's going to work. – Funk Forty Niner Jan 16 '16 at 14:06
  • 2
    Wrap off `quotes` from `table name and column name` instead use `backtick` – Saty Jan 16 '16 at 14:07
  • 1
    However, your query is failing here and you will see the error once you change the password variable. You need to remove the quotes for your table and columns. In using both my comments, that should work. Edit: just as @Saty stated; ticks, not quotes. – Funk Forty Niner Jan 16 '16 at 14:07
  • 1
    Plus use `$conn->affected_rows;` to check weather your data insert into database or not!! – Saty Jan 16 '16 at 14:12

2 Answers2

3

What is happening here is that you're using $password for both your DB and your POST array.

  • You need to change one of them as it is a conflict. I have seen this happen before.

Then your query is using regular quotes instead of ticks.

$sql = "INSERT INTO `TripBuddy`.`AppUser01` (`Name`, `Surname`, `Email`, `Password`) 

        VALUES ('$name','$surname','$email','$password');";

Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.

Edit:

As Saty stated; use mysqli_affected_rows() for truthness.


Passwords

I also noticed that you may be storing passwords in plain text. This is not recommended.

Use one of the following:

Other links:


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

  • Making sure your form does use a POST method and that your inputs all bear their matching name attributes.

I.e.: name="Name" for your POST array $_POST['Name'] etc.

  • Note that Name and name are 2 different animals and are case-sensitive.
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

seems like the password that you are providing for username root is not correct, sometimes the localhost root is set to work without password so you can try checking it with providing $password = ""; and if it returns error then you need to enter the right password to create connection

Ignis
  • 126
  • 4