0

I've gone over this script like 30 times, and I can't for the life of me find my problem. Here is the code:

function redeem() {

        $case = $_POST["case"]; 
        $name = $_POST["name"]; 
        $profession = $_POST["profession"];
        $city = $_POST["city"];
        $country = $_POST["country"];
        $totalpercent = $_POST["totalpercent"];
        $pretest = $_POST["pretest"];
        $posttest = $_POST["posttest"];
        $investigationspercent = $_POST["investigationspercent"];
        $timesreset = $_POST["timesreset"];
        $creditsspent = $_POST["creditsspent"];
        $timescompleted = $_POST["timescompleted"];

        //Add the information to the learnent_cases_leaderboard table
        $stmt = $this->db->prepare("INSERT INTO learnent_cases_leaderboard (case, name, profession, city, country, totalpercent, pretest, posttest, investigationspercent, creditsspent, timescompleted, timesreset, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)");

        $stmt->bind_param("sssssiiiiiii", $case, $name, $profession, $city, $country, $totalpercent, $pretest, $posttest, $investigationspercent, $creditsspent, $timescompleted, $timesreset); //the quotations specify the type of variable;
        //See http://php.net/manual/en/mysqli-stmt.bind-param.php for more information on bind_param
        $stmt->execute();
        $stmt->close();

When I look at the error log, it gives me this error message:

Line 105 is this line:

PHP Fatal error: Call to a member function bind_param() on a non-object on line 105

Code:

$stmt->bind_param("sssssiiiiiii", $case, $name, $profession, $city, $country, $totalpercent, $pretest, $posttest, $investigationspercent, $creditsspent, $timescompleted, $timesreset);
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Scott Kohlert
  • 413
  • 6
  • 11
  • Maybe I'm too rusty with my PHP but I think we need to see how $this->db is declared. It sounds like $this->db->prepare doesn't actually return an object. – Kris Aug 07 '11 at 04:57
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Ja͢ck Apr 22 '14 at 02:34

2 Answers2

4

You never checked that $stmt is an object. In this case, it's more likely to be FALSE, which is what PDO::prepare returns when your query has an error in it.

And your query has an error in it, because you did not delimit your field names in backticks and timestamp is a keyword.

Check for errors after invoking functions from 3rd party APIs, and fix your query.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    +1, note that "case" is also a [keyword in mysql](http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html) and thus also needs to be escaped. – vstm Aug 07 '11 at 05:01
  • it's the first field in the field list `... learnent_cases_leaderboard (case, ...` – vstm Aug 07 '11 at 05:07
  • Of course...stupid mistakes. Thanks guys! Now that I changed "case" and "timestamp" to different names that don't conflict, the script runs perfectly! – Scott Kohlert Aug 07 '11 at 05:36
1

First of; always run your queries in the localhost to see if your query executes without error. Next always make sure your the names of the fields and data types corresponds with what you have in your code

user28864
  • 3,375
  • 1
  • 25
  • 19