0

EDIT: I found the problem. The connection problem was located on the other page I was redirecting to, which had the wrong credentials, I'm an idiot.

This is my first time asking a quesiton in here, so bear with me.

I wanted to test to see whether or not I am able to insert data into my MySQL database through my .php page. Although I seemingly can't connect to my database, even though the username, password and so on are all correct. I use the same credentials, when I log on to the local instance trough MySQL Workbench.

The error i get in my browser says this:

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

Also this is my first time coding php, so the code is probably littered with errors, feel free to point them out.

Here's my code:

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

function prepareInput($input){
    $input = trim($input);
    $input = stripslashes($input);
    $input = htmlspecialchars($input);
    return $input;
}

$servername = "localhost";
$username = "root";
$password = "1234";
$dbname = "praktikdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//---------------------------------------

    $username1 = $password1 = "";
    $errUsername = $errPassword = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){

    if(empty($_POST["username"])){
        $errUsername = "Username is required";
    }
    else{
        $username1 = prepareInput($_POST["username"]);

    }

    if(empty($_POST["password"])){
        $errPassword = "Password is required";
    }
    else{
        $password1 = prepareInput($_POST["password"]);
    }
    $sql = "INSERT INTO users (username, password) VALUES('$username1', '$password1')";
    $result = $conn->query($sql);
    if(!$result) die("Something went wrong". $conn->error);
    $result->close();
    $conn->close();
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Registration</title>
    </head>
    <body>
       <form action="login.php" method="post">
            Username: <input type="text" name="username">
            <span class="error"> <?php echo $errUsername?></span>
           <br>
        Password: <input type="text" name="password">
            <span class="error"> <?php echo $errPassword?></span>
           <br>
           <input type="submit" name="btnRegister" name="Register"/>
       </form>
    </body>
</html>
BBallMick
  • 13
  • 3
  • 5
    you're using the wrong credentials... and you are vulnerable to [sql injection attacks](http://bobby-tables.com). your `prepareInput` function is useless as a defense against this. Plus, if your form wasn't filled properly, you simply go ahead with the insert query ANYWAYS, inserting bad/invalid/missing data. – Marc B Jun 07 '16 at 20:52
  • if you read the error message three times, you might see the problem. – Kevin Kopf Jun 07 '16 at 20:54
  • 2
    **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Jun 07 '16 at 21:30
  • Yes I am aware that my credentials are supposedly wrong. Otherwise the error message I get makes no sense. Although like i said in my post, they should be 100% correct, since I have no problem connecting to the same database in MySQL Workbench using the exact same credentials. That's why I'm hoping the problem lies elsewhere, like some sort of configuration. – BBallMick Jun 08 '16 at 08:27

1 Answers1

1

Your code is giving you a credentials problem (detected by your "Check connection" piece of your code), and that is derived of the users configuration in your MySQL Workbench. You need to configure the user you will use for your PHP connection to MySQL, along with the password, and the server the user connecting will be limited to (in this case is the localhost), and the privileges your user will have.

Also, and this is just for connecting to your database (for now consider that you won't execute a SQL query in your code), you can check if the connection it's ok with just your username, the password and the server name.

<?php
    /* $servername, $username and $password goes here */ 

    $conn = new mysqli($servername, $username, $password);

    if($conn->connect_error) {
         die("Connection failed:" $conn->connect_error);
    }

    echo "Connection successful";
?>
cristiancajiaos
  • 948
  • 1
  • 8
  • 16
  • Thanks, but like i said the credentials should be correct. The day before i posted this question I had no problem connecting to my database, then yesterday I couldn't, so I decided to change the password to this current one in the code. If i'm not supposed to use the same credentials when logging on my database trough MySQL Workbench, then I have no idea what credentials to use, or where to set them up. – BBallMick Jun 08 '16 at 08:10
  • I just tried testing the connection again in MySQL Workbench with the credentials shown in the code. I have no problem connecting through MySQL Workbench, although the repport window says that SSL is not enabled, I'm completely ignorant when it comes to SSL and all that, could this be the issue? – BBallMick Jun 08 '16 at 08:22