0

I'm using PDO to push some data to my MySQL server with table name 'User', and I'm getting the following error:

[26-Mar-2017 22:18:21 America/Chicago] Could not successfully add user!
[26-Mar-2017 22:18:21 America/Chicago] SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I've looked at multiple other questions on stack overflow with the same error message, and it boils down to PDO prepare and execute having mismatching parameters. But, my prepare and execute is matching, and I have checked so many times, but I'm still getting the errors. Here's my code:

        $noMusicScore = $data->noMusicScore;
        $classicalMusicScore = $data->classicalMusicScore;
        $popMusicScore = $data->popMusicScore;
        $hiphopMusicScore = $data->hiphopMusicScore;

        try
        {
            $db = new PDO('info here......');
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $query = $db->prepare("INSERT INTO User (no-music-score, classical-music-score, pop-music-score, hiphop-music-score) 
                                   VALUES (:no-music-score, :classical-music-score, :pop-music-score, :hiphop-music-score)");

            $query->execute(array(':no-music-score' => $noMusicScore, ':classical-music-score' => $classicalMusicScore, 
                                  ':pop-music-score' => $popMusicScore, ':hiphop-music-score' => $hiphopMusicScore));

            error_log("Successfully added user!");
        }

        catch(PDOException $e)
        {
            error_log("Could not successfully add user!");
            error_log($e->getMessage());
        }

        unset($db, $query);
Peter
  • 328
  • 4
  • 18
  • have you read this link http://stackoverflow.com/questions/5874383/invalid-parameter-number-parameter-was-not-defined-inserting-data – arif_suhail_123 Mar 27 '17 at 03:29
  • None of the answers or comments in that thread should be pertinent here? It seems all the parameters are present. I would suggest simplifying the query even more, just using things like :pop, :classical and so on. Just to make sure there isn't something wrong with the naming. It does look correct, though. – junkfoodjunkie Mar 27 '17 at 03:33
  • Try echo for `$query` whether it provides the required query or not? – Sachin PATIL Mar 27 '17 at 03:37
  • 1
    Don't use dashes for placeholder names. A-Z, numbers and underscores only. – Qirel Mar 27 '17 at 03:44

1 Answers1

0

I fixed the issue. I simply removed the dashes for the placeholder names and everything fell into place. 'No' is also a SQL keyword, so I think that was part of the issue as well.

Peter
  • 328
  • 4
  • 18
  • 1
    You're absolutely correct about the naming of the placeholders! But "no" is only a keyword, not a reserved one, so thar world work :-) but columns shouldn't contain dashes either, mysql will think you're doing math! – Qirel Mar 27 '17 at 03:48