3

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 :/

22
  • is $query -> bind_param("ss", $booking_id, $desk_id); the 80-th line in functions.php? Commented Jan 13, 2013 at 1:07
  • Yes. Sorry should have mentioned that. Commented Jan 13, 2013 at 1:08
  • and what your PDO::ERRMODE set to? (PDO::ERRMODE_SILENT;PDO::ERRMODE_WARNING;PDO::ERRMODE_EXCEPTION) Commented Jan 13, 2013 at 1:14
  • 1
    As the error suggests your prepare statement probably fails. Try var_dump($query) right after prepare Commented Jan 13, 2013 at 1:19
  • 2
    Just a check but, Are you intentionally calling delBooking() with 4 arguments even though only 2 arguments are enamed in the function definition? Commented Jan 13, 2013 at 1:34

1 Answer 1

3

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(....);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.