Execute Statement error

Execute Statement error
0.0 0

#1

I have only slightly modified this code from the tutorial and code at the following page.

https://devdojo.com/episode/create-a-php-login-script

The form loads fine but I get an error on execution. If I comment out the if statement containing the execute function the code doesn’t throw errors. I use Yahoo webhosting (not the best) and the newest php version they support is 5.3.6. Is there something about using PDO that wouldn’t be supported by this version? Aside from the fact that I am not using password_hash in this code?

<?php

session_start();

require 'config.php';

$msg = '';

if(!empty($_POST['userEmail']) && !empty($_POST['userPass'])):

    $sql = "INSERT INTO users (userEmail, userPass) VALUES (:userEmail, :userPass)";
    
    $db = getDB();
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':userEmail', $userEmail);
    $stmt->bindParam(':userPass', $userPass);

    $userEmail = $_POST['userEmail'];
    $userPass = $_POST['userPass'];
    echo $sql;
    if( $stmt->execute() ):
        $msg = 'Successfully created new user';
    else:
        $msg = 'There was an error creating the new account';
    endif;
endif;

 
?>

#2

What is the exact error your are getting?


#3

I don’t have an IDE. I’m just editing the code in a txt editor and running it in the browser. I’m just getting the typical website cannot display the page “The website has a programming error.” note.


#4

You will need to configure your php.ini to show errors on the page.


#5

access to the php.ini file is disabled on Yahoo web hosting. Is there a workaround to set this?


#6

Try adding the following to the top of the php file.

error_reporting(E_ALL);
ini_set("display_errors", 1);

Also, check your browser’s console from JavaScript errors that may be showing. Use Ctrl+Shft+J in Chrome to view the console.


#7

That worked!

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘’ for key ‘user_name’’ in /register.php:24 Stack trace: #0 /register.php(24): PDOStatement->execute() #1 {main} thrown in /register.php on line 24


#8

Now you have a way to track down the error.


#9

That is a huge help. Thanks again.

It looks like I’m trying to duplicate a value in the database that requires a unique entry. I will have to validate the input first.