0

I have this small function which might insert users into the database. I'm using PDO and bindValue. Without the binding all works, in this way the connection with the database starts but the query does nothing. Where is the problem with my code?

// [INSERIMENTO] Aggiunge i nuovi dati al database
function inserisciUtente($idFacebook,$nome,$cognome){
    $QUERY="INSERT INTO giocatore(id, nome, cognome, foto)
           VALUE(:id, :nome, :cognome, :foto)";
    try{
        $dbh=db_connect();
        $stmt=$dbh->prepare($QUERY);

        $stmt->bindValue(':id', $idFacebook, PDO::PARAM_INT);
        $stmt->bindValue(':nome', $nome, PDO::PARAM_STR, 32);
        $stmt->bindValue(':cognome', $cognome, PDO::PARAM_STR, 32);
        $stmt->bindValue(':foto', "https://graph.facebook.com/".$idFacebook."/picture?width=50&height=50", PDO::PARAM_STR, 256);

        $stmt->execute();
        $dbh=null;
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
}  
Michele Benolli
  • 55
  • 1
  • 11
  • Display errors: `if(!$stmt->execute()){ print_r($stmt->errorInfo()) };` – turson Jul 21 '14 at 10:28
  • You can make your questions title much more productive – Mr. Alien Jul 21 '14 at 10:29
  • It doesn't work without the insertion of the photo Url too.. – Michele Benolli Jul 21 '14 at 10:29
  • Ok, thank you for the reply. I try to insert the piece of code posted by turson and what I have is an SQLSTATE HY093 on the ID, a numeric error or something similar. I don't find where I fault with the ID. If I put 45 or 121254615 instead of $idFacebook I have the same error. – Michele Benolli Jul 21 '14 at 11:47
  • What happens if you dont set the type ?`$stmt->bindValue(':id', $idFacebook)` . is id the primary key of your table? if so Its not a good idea to make it the facebook Id, you should have a facebook_id field and let the table auto increment the key – meda Jul 21 '14 at 12:53
  • If I don't set the type is the same: Array ( [0] => HY093 [1] => [2] => ). Naturally Id is the primary key. I read now this http://stackoverflow.com/questions/6803469/rely-on-facebook-user-id-as-a-permanent-user-identifier, ok, the use of FBid is not he best choice, but if I insert a number the error remains... – Michele Benolli Jul 21 '14 at 13:10

0 Answers0