skip to Main Content

I’m following a video on how to create a login system and I came accross this error and I have no idea what’s wrong so I’d really appreciate some help!

The error:

Registration failed.SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '. '[email protected]', 'mypassword', now())' at line 2

My code:

<?php

include_once 'resources/Database.php';


if(isset($_POST['email'])){
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    try{
         $sql = "INSERT INTO users (username, email, password, regDate)
         VALUES (:username. :email, :password, now())";
        
        $statement = $db->prepare($sql);
        $statement->execute(array(':username' => $username, 'email' => $email, 'password' => $password));
        
        if($statement->rowCount() ==1){
            $result = "<p style='padding: 20px; color: green;'> Registration Successful</p>";
        }
    } 
    catch (PDOException $ex){
        $result = "<p style='padding: 20px; color: red;'> Registration failed." .$ex->getMessage() ."</p>";
        
    }
        
    
}

?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Sign Up</title>
    </head>

    <body>

        
        <h3>Sign up</h3>
        
        <?php if(isset($result)) echo $result; ?>
        <form method="post" action="">
            <table>
                <tr>
                    <td>Email:</td>
                    <td><input type="text" value="" name="email"></td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td><input type="text" value="" name="username"></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" value="" name="password"></td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" value="Sign Up">
                    </td>
                </tr>
            </table>
        </form>
        
        
        <p>Go <a href="index.php">back</a></p>
        
        
        <?php include_once 'resources/Database.php' ?>
    </body>
</html>

What I don’t understand is, it mentions MariaDB in the error. But I’m using MySQL from XAMP
Is that the cause? if so how can I fix it?

Thanks a lot!

2

Answers


  1. You have a problem here. replace . next to :username bind param with a ,

    Corrected code : –

    $sql = "INSERT INTO users (username, email, password, regDate)
             VALUES (:username, :email, :password, now())";
    
    Login or Signup to reply.
  2. I see two mistakes. To fix it:

    1. Change ":username." to ":username," at string

    VALUES (:username. :email, :password, now())";

    1. change

    ’email’ => $email, ‘password’ => $password

    to

    ‘:email’ => $email, ‘:password’ => $password

    at string

    $statement->execute(array(‘:username’ => $username, ’email’ => $email, ‘password’ => $password));

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search