mikenike012
mikenike012

Reputation: 1

Resolving a PHP PDO Error: SQLSTATE[42000] [1044]

Found lots of similar problems on this site, but the solutions for those issues don't seem to reply. The user in question has full access to the database, and from what I can tell I'm not missing any commas etc. A second set of eyes would be great.

Submitted signature is in an acceptable formatTrying to open a connectionError!: SQLSTATE[42000] [1044] Access denied for user 'emkinsti_user1'@'localhost' to database 'signatures'

<?php
// Tracks what fields have validation errors
$errors    = array();
// Default to showing the form
$show_form = true;

// 1. Get the input from the form
//  Using the PHP filters are the most secure way of doing it
$name   = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$output = filter_input(INPUT_POST, 'output', FILTER_UNSAFE_RAW);

// 2. Confirm the form was submitted before doing anything else
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

// 3. Validate that a name was typed in
if (empty($name)) {
    $errors['name'] = true;
}

// 3. Validate that the submitted signature is in an acceptable format
if (!json_decode($output)) {
    $errors['output'] = true;
}
}

// No validation errors exist, so we can start the database stuff
if (empty($errors)) {

echo "Submitted signature is in an acceptable format";"<br/>";

$dsn  = 'mysql:host=localhost;dbname=signatures';
$user = 'emkinsti_user1';
$pass = '6nqq103t26';
}
// 4. Open a connection to the database using PDO
try {
echo "Trying to open a connection";
$db = new PDO($dsn, $user, $pass);
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

// Make sure we are talking to the database in UTF-8
$db->exec('SET NAMES utf8');

// Create some other pieces of information about the user
// to confirm the legitimacy of their signature
$sig_hash = sha1($output);
$created  = time();
$ip       = $_SERVER['REMOTE_ADDR'];


// 5. Use PDO prepare to insert all the information into the database
$sql = $db->prepare('INSERT INTO signatures (signator, signature, sig_hash,    ip, created)
VALUES (:signator, :signature, :sig_hash, :ip, :created)');
$sql->bindValue(':signator', $name, PDO::PARAM_STR);
$sql->bindValue(':signature', $output, PDO::PARAM_STR);
$sql->bindValue(':sig_hash', $sig_hash, PDO::PARAM_STR);
$sql->bindValue(':ip', $ip, PDO::PARAM_STR);
$sql->bindValue(':created', $created, PDO::PARAM_INT);
$sql->execute();

// 6. Trigger the display of the signature regeneration
$show_form = false;
//  mysql_close($db);
$db = null;
?>

Upvotes: 0

Views: 1590

Answers (2)

Clark Superman
Clark Superman

Reputation: 373

emkinsti_user1'@'localhost' to database 'signatures' if you are using CPanel, CPanel uses prefixes also to the database name:

You used: emkinsti_user1 as users.

You should use: emkinsti_signatures as database name.

Log in into your CPanel and there you will find the database name with prefix

Upvotes: 1

KIKO Software
KIKO Software

Reputation: 16741

Try http://php.net/manual/en/pdo.getavailabledrivers.php to see if the database is supported by PDO.

<?php
print_r(PDO::getAvailableDrivers());
?>

Just an idea. I would expect another error message when it isn't. So, as far as I can tell, the user has no access when accessing the database from the local host.

Upvotes: 0

Related Questions