Reputation: 596
I am writing a program that will receive values through URL and then will either add an entry to the MySQL database, delete it or if someone else has booked that slot then it will not let you delete it.
The add and delete functions are as follows:
function addBooking($id, $desk, $member, $date){
global $dbconn;
$query = $dbconn -> prepare("INSERT INTO booked (booking_id, desk_id, member_id, date_booked) VALUES (?,?,?,?)");
$query -> bind_param("iiis", $id, $desk, $member, $date);
$query->execute();
print '<div class="yours" title="Your Booking">Your Booking</div>';
$query -> close();
}
function delBooking($id, $desk){
global $dbconn;
$query = $dbconn -> prepare("DELETE FROM booked WHERE booking_id = ? AND desk_id = ?");
$query -> bind_param("ii", $id, $desk);
$query->execute();
print '<div class="free" title="Click To Book">Not Booked</div>';
$query -> close();
}
And the code that calls them is this:
$query = $dbconn -> prepare("SELECT firstname, lastname, booked.member_id, date_booked FROM members, booked WHERE (members.member_id = booked.member_id) AND booking_id = ? AND desk_id=?");
$query -> bind_param("ss", $booking_id, $desk_id);
if($query->execute() == true) {
$query -> bind_result($fname, $lname, $memid, $dbooked);
$query -> fetch();
if($fname){
if ($memid == $member_id)
{
delBooking($booking_id,$desk_id);
}
else
{
print '<div class="taken" title="Booked by <strong>'.$fname.' '.$lname.'</strong><br>On '.$dbooked.'">Booked</div>';
}
}else{
addBooking($booking_id,$desk_id,$member_id,$date);
}
}
It adds a booking fine and will also tell you if someone else has booked it fine, but if you run it again on your own booking it will give you the error:
Fatal error: Call to a member function bind_param() on a non-object in /var/www/new/functions/functions.php on line 80
I'm not sure why. It's not my delete statement or anything because I can rename the addBooking function to delBooking so it's identical working code and it still gives me exactly the same error :/
Upvotes: 3
Views: 712
Reputation: 41428
When using mysqli, you cannot have more than one prepared statement active on a single connection at the same time. By active I mean is has been sent over to the server by calling it's execute() at least once. You can have as many unused statements prepared, but you can only execute on one at a time. When done, you need to close the statement before you execute another. Before you call delBooking() close the earlier statement:
$query->fetch();
$query->close();
if($memid == $member_id){
delBooking(....);
Upvotes: 3