2

I would like to UPDATE the seat_status to active by checking two conditions.

1st condition = bus_id

2nd condition = seat_title

I'm using this code in function cancelbook.

function cancelbook($conn,$id,$busid)
{
    $stmtgetseats = $conn->prepare("SELECT seat_no from tbl_seats WHERE bus_id=:bus_id");
    $stmtgetseats->bindParam(':bus_id',$busid);
    $stmtgetseats->execute();
    $seat_no=$stmtgetseats->fetchAll();
    for($i=0;$i<count($seat_no);$i++)
    {
        $stmtactive = $conn->prepare("UPDATE tbl_busseats SET seat_status='active' WHERE bus_id=:bus_id AND seat_title=:seat_title");
        $stmtactive->bindParam('bus_id',$busid);
        $stmtactive->bindParam('seat_title',$seat_no[$i]);
    }
    if ($stmtactive->execute()) {
        exit();
        return true;
    }
    return false;
}

im getting this error Notice: Array to string conversion

5
  • the last if statement doesn't look right !, exit() will terminate script execution, did you mean if ($stmtactive->execute()) return true; return false; Commented Jun 10, 2018 at 13:25
  • in which line ? please provide the full notice Commented Jun 10, 2018 at 13:25
  • I exited to check error Commented Jun 10, 2018 at 13:33
  • The error is from line where the seat_title is binded Commented Jun 10, 2018 at 13:33
  • which means that $seat_no[$i] is not a string but an array, can you do a var_dump($seat_no); Commented Jun 10, 2018 at 13:35

2 Answers 2

1

The way you did the loop will only update one row, to update every row you should execute your statement for each iteration.

function cancelbook($conn,$id,$busid)
{
    $stmtgetseats = $conn->prepare("SELECT seat_no from tbl_seats 
                                    WHERE bus_id=:bus_id");
    $stmtgetseats->bindParam(':bus_id',$busid);
    $stmtgetseats->execute();
    $seat_no=$stmtgetseats->fetchAll();
    foreach($seat_no as $seat) {
        $stmtactive = $conn->prepare("UPDATE tbl_busseats SET seat_status='active' 
                                      WHERE bus_id=:bus_id 
                                      AND seat_title=:seat_title");
        $stmtactive->bindParam('bus_id',$busid);
        $stmtactive->bindParam('seat_title',$seat['seat_no']);
        $stmtactive->execute();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

if you need to keep track of the number of successful updates and failures, i'll update the answer accordingly.
what is difference between your solution and mine?
read the OP's question and my answer, i'm sure you'll figure it out :)
0

it seems $seat_no[$i] is not string and it's an array , (I suggest var_dump($seat_no[$i]); it before), my quess is it should be something the following snippet:

use

$stmtactive->bindParam('seat_title',$seat_no[$i]['seat_no']);

instead of

$stmtactive->bindParam('seat_title',$seat_no[$i]);

in your code will resolved your problem.

of course, it's better instead of using for learn to use foreach

But another solution with better performance is using just one update instead of serveral updates!!!

function cancelbook($conn,$id,$busid)
{
    $stmtgetseats = $conn->prepare("SELECT seat_no from tbl_seats 
                                    WHERE bus_id=:bus_id");
    $stmtgetseats->bindParam(':bus_id',$busid);
    $stmtgetseats->execute();
    $seat_no=$stmtgetseats->fetchAll();
    $seat_numbers = array_values($seat_no);
    $stmtactive = $conn->prepare("UPDATE tbl_busseats SET seat_status='active' 
                                      WHERE bus_id=:bus_id 
                                      AND seat_title IN (:seat_title"));
    $stmtactive->bindParam('bus_id',$busid);
    $stmtactive->bindParam('seat_title',implode(",",$seat_numbers));
    $stmtactive->execute();
}

Also, remove Exit in your code, it's a bad mistake in your code

2 Comments

i need to check value from that array to string
@SoonangRai I mean you just need this line, why I should rewrite the whole code of you? I answered this question before another answer but you mark that as answer!!!! why? the solutions is the same

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.