0

I want to insert a value from my form into my SQL table.

Here's the form:

   <form action="index.php?id=" method="GET">
     <input id="Trigramme" name="Trigramme" type="text" value="<?php 
      if (empty($_GET['Trigramme'])):
          echo ('');
      else:
           echo ($_GET['Trigramme']);
      endif;
       ?>">
     <input id='idFormation' type="hidden" name="id" value="<?php 
      if ($formations['ID'] <= $maxIdFormation['Max_ID']):
          echo ($formations['ID']+1);
      else:
           echo ('1');
      endif;
       ?>">
     <input type="submit">
   </form>

And here's the function I use for the insertion:

function ajouterMembre($infosMembre){
    $pdo = getDb(); 

$requeteAjout = $pdo -> prepare ("INSERT INTO `users` ( `Trigramme`, `Formation_id`) VALUES (':Trigramme', ':Formation_id')");

$requeteAjout -> bindValue(':Trigramme', $infosMembre['Trigramme']);
$requeteAjout -> bindValue(':Formation_id', $infosMembre['id']);

$requeteAjout -> execute();

var_dump($requeteAjout);
var_dump( $infosMembre['id']);
var_dump( $infosMembre['Trigramme']);

return $pdo-> lastInsertId();

}

And lastly, this is the function I use in my HTML:

<?php $idNouveauMembre = ajouterMembre($_GET); ?>

The issue I have is that the SQL returns the values Trigramme = :Trigramme and id = 0.

I guess that my bindValue is not working properly. But when I try to check with:

var_dump( $infosMembre['id']);
var_dump( $infosMembre['Trigramme']);

it returns the correct values I want inserted.

robinCTS
  • 5,746
  • 14
  • 30
  • 37
  • 2
    You are not referencing the variable type.. see php manual [documentation](http://php.net/manual/en/pdostatement.bindvalue.php) and try again – Noel Dec 15 '17 at 10:26
  • You don't need the apostrophes in your query. VALUES (:Trigramme, :Formation_id) – user1915746 Dec 15 '17 at 10:40

3 Answers3

0
$requeteAjout = $pdo -> prepare ("INSERT INTO `users` ( `Trigramme`, `Formation_id`) VALUES (':Trigramme', ':Formation_id')");

replace that line of code with this

$requeteAjout = $pdo -> prepare("INSERT INTO `users` ( `Trigramme`, `Formation_id`) VALUES (:Trigramme, :Formation_id)");
Sébastien
  • 11,860
  • 11
  • 58
  • 78
desmond
  • 1
  • 2
0

Dont add quotation marks around this :Formation_id and the other one too.

Gardezi
  • 2,692
  • 2
  • 32
  • 62
0

remove single quotes in your prepare statement value:

$requeteAjout = $pdo -> prepare ("INSERT INTO `users` ( `Trigramme`, `Formation_id`) VALUES (:Trigramme, :Formation_id)");
mehrdadep
  • 972
  • 1
  • 15
  • 37