Tycho Atsma
Tycho Atsma

Reputation: 61

exception 'PDOException' with message 'could not find driver'

I have searched the web for solutions for this well known problem, but all of them seem to be caused by the fact that the mysql driver is not installed. I checked for myself and i have mysql installed so i dont know what's causing this problem. My code:

<?php

try 
{
    $db = new PDO('myqsl:host=localhost, dbname=stagepeer', 'root', 'root');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    $sql = $db->prepare("INSERT INTO werknemers (naam, achternaam, wachtwoord, telefoonnummer, plaatsnaam, email) VALUES(:naam, :achternaam, :wachtwoord, :telefoonnnummer, :plaatsnaam, :email)");
    $sql->execute(array(":naam"=>$voornaam, ":achternaam"=>$achternaam, ":wachtwoord"=>$wachtwoord, ":telefoonnummer"=>$telefoonnummer, ":plaatsnaam"=>$plaatsnaam, ":email"=>$email));


if (empty($_POST['voornaam']))
{
    echo "Voornaam ontbreekt" . "<br>";
} 
else
{
    $voornaam = $_POST['voornaam'];
}
if (empty($_POST['achternaam']))
{
    echo "Achternaam ontbreekt" . "<br>";
} 
else
{
    $achternaam = $_POST['achternaam'];
}
if (empty($_POST['plaatsnaam']))
{
    echo "Plaatsnaam ontbreekt" . "<br>";
} 
else
{
    $plaatsnaam = $_POST['plaatsnaam'];
}
if (empty($_POST['gebruikersnaam']))
{
    echo "Email ontbreekt" . "<br>";
} 
else
{
    $email = $_POST['gebruikersnaam'];
}
if (empty($_POST['telefoon']))
{
    echo "Telefoonnummer ontbreekt" . "<br>";
} 
else
{
    $telefoon = $_POST['telefoon'];
}
if (empty($_POST['wachtwoord']))
{
    echo "Wachtwoord ontbreekt" . "<br>";
} 
else
{
    $wachtwoord = $_POST['wachtwoord'];
}

}
catch(PDOException $ex) 
{
    echo $ex . "error";
}  
?>

This is the complete error

exception 'PDOException' with message 'could not find driver' in /Applications/MAMP/htdocs/Vacaturesite/registratie.php:5 Stack trace: #0 /Applications/MAMP/htdocs/Vacaturesite/registratie.php(5): PDO->__construct('myqsl:host=loca...', 'root', 'root') #1 {main}error

Upvotes: 1

Views: 1493

Answers (1)

meda
meda

Reputation: 45490

This is a simple error, I see that you fixed the driver error. However you ran the query before assigning the variables, they are undefined at that point:

  • You should adjust your code to query after validation not the opposite
  • Use isset()
  • Avoid unnecessary variables

<?php
if (isset($_POST['voornaam'], $_POST['achternaam'], $_POST['plaatsnaam'], $_POST['gebruikersnaam'], $_POST['telefoon'], $_POST['wachtwoord']))
{
$params = array(":naam"=>$_POST['voornaam'], 
                ":achternaam"=>$_POST['achternaam'], 
                ":wachtwoord"=>$_POST['plaatsnaam'], 
                ":telefoonnummer"=>$_POST['gebruikersnaam'], 
                ":plaatsnaam"=>$_POST['telefoon'], 
                ":email"=>$_POST['wachtwoord'])

    try 
    {
        $db = new PDO('myqsl:host=localhost, dbname=stagepeer', 'root', 'root');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

        $sql = $db->prepare("INSERT INTO werknemers (naam, achternaam, wachtwoord, telefoonnummer, plaatsnaam, email) VALUES(:naam, :achternaam, :wachtwoord, :telefoonnnummer, :plaatsnaam, :email)");
        $sql->execute($params);
    }catch(PDOException $ex) 
    {
        echo $ex . "error";
    }  
}else{
    echo "Fields ontbreekt" . "<br>";
}
?>

Upvotes: 1

Related Questions