Home > Software design >  mySql and php add to database
mySql and php add to database

Time:03-04

I've been stuck on this for hours. I have to manipulate a database with a website using php. I'm trying to add data from a form to my database but it gives me : PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in...

This is what's creating the problem.

$sqlQuery = 'INSERT INTO succursale(adresse, ouverture, fermeture, ouvert_raison) VALUES (:adresse, :ouverture, :fermeture, :ouvert_raison)';
$req = $conn->prepare($sqlQuery);
$req-> execute(array($_POST['adresse'], $_POST['ouverture'], $_POST['fermeture'], $_POST['ouvert_raison']));;

I've looked for people with a similar problem but the answers given do not match with my situation.

CodePudding user response:

You are using named placeholder in your prepared statement. This means you'll need to pass those as an associative index. Otherwise the statement will not know which value to bind to the placeholder

$req-> execute(array(
    ':adresse'        => $_POST['adresse'], 
    ':ouverture'      => $_POST['ouverture'],
    ':fermeture'      => $_POST['fermeture'],
    ':ouvert_raison'  => $_POST['ouvert_raison'],
));

If you don't want to do this, you can change the named placeholders to a ?

$sqlQuery = 'INSERT INTO succursale(adresse, ouverture, fermeture, ouvert_raison) VALUES (?, ?, ?, ?)';
  • Related