1

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
4
  • 2
    costly typo: myqsl: != mysql and dsn delimiters are ; Commented Jan 14, 2015 at 15:40
  • I changed the delimiters but that did not changed anything Commented Jan 14, 2015 at 15:41
  • Run the setup program (for PHP, not just the MySQL server), make sure you have the mysql part of PDO installed in the PHP components list. Commented Jan 14, 2015 at 16:04
  • Already solved it, now i got an invalid parameter error [SQLSTATE (HY093)] Commented Jan 14, 2015 at 16:08

1 Answer 1

1

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>";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.