2

I've been pulling my hair out on this MySQL query.

Let's say I have this:

    $add = "INSERT INTO books (title) VALUES(?)";
    if ($stmt = $mysqli->prepare($add)) {

        $arr = array($title);

        foreach ($arr as $value) {
            echo var_dump($value);
        }

        $stmt->bind_param("s", $title);

With that foreach -> var_dump :

string 'Medieval Times (History)' (length=24)
int 1422843281
int 1420844341
string '127.0.0.1' (length=9)
string 'MY_EMAIL@gmail.com' (length=22)
string '' (length=0)
int 1420844805
int 6
int 3
int 1
int 0
int 0
int 1
int 1
int 1
int 1

Well, it stops when it hits this line and I get this error:

Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\books\dashboard.php on line 386

With line 386: $stmt->bind_param ...

So, I know I am importing 16 variables yet ... I get this error. Argh.

Zeng Cheng
  • 755
  • 2
  • 7
  • 16

5 Answers5

7

TL\DR

Your query is failing to prepare(). You need to figure out where, how and why. Look at the last code block of this answer and let us know what the error is.


I'll start with the query. You're trying to access a MySQL reserved word Source (See #684). You need to wrap those in backticks like this:

$add = "INSERT INTO books (title, edited, created, ip,".
    " email_to, twitter, last_taken, questions_total, responses, ".
    "show_progress, need_correct, go_back, state, send_stats, ".
    "show_number, imported) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ".
    "?, ?, ?, ?, ?, ?, ?)";

Now, you're instantiating the variable $stmt within the if block but then trying to bind it outside of that block. You'll need to change this:

if ($stmt = $mysqli->prepare($add)) {
....
}
$stmt->bind_param(....);

To this:

if ($stmt = $mysqli->prepare($add)) {
....
$stmt->bind_param(....);
}

Also, make sure your query is actually preparing correctly:

if ($stmt = $mysqli->prepare($add)) {

    $stmt->bind_param("siisssiiiiiiiiii", $title, $edited, $created, $ip, $email_to, $twitter, $last_taken, $questions_total, $responses, $show_progress, $need_correct, $go_back, $state, $send_stats, $show_number, $importedVal);

    // execute it and all...
} else {
    die("Errormessage: ". $mysqli->error);
}

Then let us know what turns up.

Darren
  • 13,050
  • 4
  • 41
  • 79
0

Try the following

$stmt->bind_param(array("siisssiiiiiiiiii", $title, $edited, $created, $ip, $email_to, $twitter, $last_taken, $questions_total, $responses, $show_progress, $need_correct, $go_back, $state, $send_stats, $show_number, $importedVal));
Darren
  • 13,050
  • 4
  • 41
  • 79
EthanHu
  • 87
  • 2
  • 3
  • 12
0

Edited: try this

if ($stmt = $mysqli->prepare($add)) {

    $stmt->bind_param($arr);
}
else {
    printf("Errormessage: %s\n", $mysqli->error);
}

http://php.net/manual/en/mysqli-stmt.bind-param.php

mehany
  • 3,747
  • 2
  • 17
  • 28
  • 2
    There's actually 16 in the query and the binds. – Darren Feb 02 '15 at 03:23
  • 1
    Where is the OP's 17? I only count 16. – Sean Feb 02 '15 at 03:32
  • I usually bind all parameters and not some. The error is indicating that the statement fails to prepare, so the statement need to be checked for validation anyways as you indicated in your answer – mehany Feb 02 '15 at 05:08
0

Two things maybe.

  1. Is $mysqli out of scope? Other words, are you using $mysqli in a function but not passed to that function or it's not been declared global in that function?
  2. In your table, is any of the columns of INT not an INT? could any of them be a tinyint,smallint, or mediumint? Then your values are too big.
worldofjr
  • 3,868
  • 8
  • 37
  • 49
Zoltar
  • 232
  • 1
  • 4
  • 10
0

You have a problem with your prepared statement because of an error in your SQL. As you have underscores in your field names, I almost guarantee that the problem is the prepare is throwing a wobbly over you not wrapping them in ` marks (I forget the actual name of the flicky apostrophe). You therefore need;

$add = "INSERT INTO books (title, edited, created, ip,".
  " `email_to`, `twitter`, `last_taken`, `questions_total`, `responses`, ".
  "`show_progress`, `need_correct`, `go_back`, `state`, `send_stats`, ".
  "`show_number`, `imported`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ".
  "?, ?, ?, ?, ?, ?, ?)";

I have wrapped all the fields with the marks as it is best practice, but in reality it's the special character that causes the problem in my experience.

Hope this helps.

worldofjr
  • 3,868
  • 8
  • 37
  • 49