Trying to input text into database

ello,
I’m trying to just get my db running by imputing some simple info, firstname and lastname.
I keep getting the error:

Connected successfullyError: INSERT INTO nametable (firstname, lastname) VALUES(‘asdf’,‘fasd’)
Access denied for user ‘id1759438_gannon’@’%’ to database ‘information_schema’

Apparently the connection test is working.
Here is the code for insert.php (minus the password):

<?php
$servername = "localhost";
$username = "id1759438_gannon";
$dbname = "id1759438_testinputs";
$password = "**********";

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

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

//insert values
$sql="INSERT INTO nametable (firstname, lastname)
VALUES('$_POST[fname]','$_POST[lname]')";

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

Here is my index.php that passes the form text:

<h1>An example page to insert some data into MySQL database using PHP</h1>
<form action="insert.php" method="post">
Firstname: <input type="text" name="fname" /><br><br>
Lastname: <input type="text" name="lname" /><br><br>
<input type="submit" />
</form>

My db is all set with firstname and lastname as VARCHAR(20) and everything!
Please help! Thanks!

As it says your accessing wrong database.

See PDO example

Declare username and database

This topic was automatically closed after 22 hours. New replies are no longer allowed.