ThorDozer
ThorDozer

Reputation: 104

Action in a DATABASE with PHP

I have an error when i try to connect my database.

Error : SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I doubt it was the ID but with phpMyAdmin I put it AUTOINCREMENT ...

Here's my code -->

From the DATABASE ON PHPMYADMIN

CREATE TABLE  `Compte_Utilisateur`.`info_compte` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 15 ) NOT NULL ,
`suffixe` VARCHAR( 20 ) NOT NULL ,
`password` VARCHAR( 15 ) NOT NULL ,
`siteWeb` VARCHAR( 20 ) NOT NULL ,
`fonction` VARCHAR( 10 ) NOT NULL
) ENGINE = INNODB;

And from my file php --> I want to ADD a User to the DATABASE (But YOU ALL ALREADY KNOW :D)

<?php
$username = $_POST["username"];
$suffixe = $_POST["suffixe"];
$passwd = $_POST["password"];
$site = $_POST["site"];
$fonction = $_POST["fonction"];

try
{
    $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
    $bdd = new PDO('mysql:host=localhost;dbname=compte_utilisateur', 'root', '', $pdo_options);

    $req = $bdd->prepare('INSERT INTO info_compte(username, suffixe, password, siteWeb, fonction) 
                            VALUES(:username, :suffixe, :passwd, :site, :fonction)');
    $req->execute(array(
    'username' => $username,
    'suffixe' => $suffixe,
    'password' => $passwd,
    'siteWeb' => $site,
    'fonction' => $fonction,
    ));
    echo 'Compte ajouté avec succès';
}
catch(Exception $e)
{
        die('Erreur : ' . $e->getMessage());
}
?>

Thanks to answer !

Upvotes: 0

Views: 92

Answers (3)

allnightgrocery
allnightgrocery

Reputation: 1380

Are your binders matching? "username", "suffixe" and "fonction" look good but "password" and "siteWeb" look off. Your prepare shows "passwd" and "site" but you're using "password" and "siteWeb" as your binding keys.

$req->execute(array(
    'username' => $username,
    'suffixe' => $suffixe,
    'passwd' => $passwd,
    'site' => $site,
    'fonction' => $fonction));

Upvotes: 2

bw_&#252;ezi
bw_&#252;ezi

Reputation: 4564

(no php here to test...)

remove the last comma in the execute array:

$req->execute(array(
'username' => $username,
'suffixe' => $suffixe,
'password' => $passwd,
'siteWeb' => $site,
'fonction' => $fonction        // <-- no comma here... 
));

or your array will include an additional null element and thus be too large.

Upvotes: 1

bw_&#252;ezi
bw_&#252;ezi

Reputation: 4564

I think the problem is that mysql doesn't support named parameters in the prepared sql.
use ? instead of :name (and also remove these names form the array)

update: looks like this is not a problem with a PDO driver (I'm more familiar with JDBC drivers...)

Upvotes: 0

Related Questions