0

I have two subsequent mysqli statements, and the second returns:

Fatal error: Call to a member function bind_param() on a non-object in ...

Why this happens? Does this means that I need to open two different connection? Is there any way to avoid this (I love keeping the SQL connection details in one file)?

Here the code:

$db = new mysqli("localhost", "root", "", "database");

$stmt = $db->prepare("UPDATE posts SET vote_".$_POST['vote']." = vote_".$_POST['vote']." + 1 WHERE id=?");
$stmt->bind_param('s', $_POST['id_post']);
$stmt->execute();
$stmt->close();

$stmt = $db->prepare("INSERT INTO votes (kind, users_id, posts_id) VALUES (?, ?, ?)");
$stmt->bind_param('sss',$_POST['vote'],$_POST['id_user'],$_POST['id_post']);
$stmt->execute();
$stmt->close();
Tomalak
  • 332,285
  • 67
  • 532
  • 628
0plus1
  • 4,475
  • 12
  • 47
  • 89

3 Answers3

1

I think your $stmt variable is null when you call bind_param over it. maybe your $_POST['vote'] is empty? you can check it before you bind the param on the command

Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
1

Check the return value of mysqli::prepare. If it is FALSE, you should get the details for the occured error with mysqli::error.

joschi
  • 12,746
  • 4
  • 44
  • 50
0

Something might have gone wrong with $db->prepare(), check $db->error.

Jaka Jančar
  • 11,386
  • 9
  • 53
  • 74
  • That was the problem, I named differently one of the fields.. shame on me for not checking the error.. I thought that was a problem of making two consecutive queries.. thanks! – 0plus1 Apr 07 '09 at 13:52