1
if (isset($_POST['finalSeats']))
{

getting variable from post

$TicketType =$_POST['Tickettype'];
$seatS=$_POST['finalSeats'];
$EventId=$_POST["Eventid"];

array getting from last page

$cats = array_filter(array_map('trim', explode(',', $seatS)));
$sqlData = array();

getting values one by one with foreach

foreach($cats as $key => $cat ) {
$stmt = $con->prepare("SELECT * FROM fistevent WHERE `Event_Id`=? AND `TicketType`=? AND `seats`= ?") or die($con->error);
$stmt->bind_param("sss",$EventId , $TicketType,$cat);
$stmt->execute();
$stmt->store_result();

Bind the results

$stmt-> bind_result($id,$Event_Id,$TicketType,$row_name,$seats,$Status);
while($stmt->fetch())
{
$data[] = array($id,$Event_Id,$TicketType,$row_name,$seats,$Status);
$Tickettype=$TicketType;
$Rowname=$row_name;
$Seats=$seats;
$status=$Status;

Comparing values array and database values

if($Seats===$cat && $status==='Hold')
{
echo'<script>window.location="selection.php";</script>';
}

if not matched update the query

else
{
$seatS=$_POST['finalSeats'];
$Eventid=$_POST["Eventid"];
$_SESSION['Eventid']=$Eventid;
$cats = array_filter(array_map('trim', explode(',', $seatS)));
$stmt = $con->prepare('UPDATE fistevent SET `Status`="Hold" where `Event_Id`=? AND `seats`= ? ') or die($con->error);
$stmt->bind_param("ss", $_POST['Eventid'], $cat);
foreach($cats as $key => $cat ) {
$stmt->execute();
} 
}

}
}
}

1 Answer 1

1

Based on your updated code snippets, you need to change couple of things in your code, such as:

  • Change your prepared statement in the following way,

    $stmt = $con->prepare("SELECT * FROM fistevent WHERE `Event_Id`=? AND `TicketType`=? AND `seats`= ? AND status='Hold'") or die($con->error);
    

    This way, you don't have to use this if($Seats===$cat && $status==='Hold') conditional or while($stmt->fetch()){ ... statements in your code.

  • And after binding the result, simply do this:

    ...
    $stmt->bind_result($id,$Event_Id,$TicketType,$row_name,$seats,$Status);
    if($stmt->num_rows){
        header("Location: selection.php");
        exit();
    }else{
        $seatS=$_POST['finalSeats'];
        $Eventid=$_POST["Eventid"];
        $_SESSION['Eventid']=$Eventid;
        $cats = array_filter(array_map('trim', explode(',', $seatS)));
        $stmt = $con->prepare('UPDATE fistevent SET `Status`="Hold" where `Event_Id`=? AND `seats`= ? ') or die($con->error);
        foreach($cats as $cat ) {
            $stmt->bind_param("ss", $Eventid, $cat);
            $stmt->execute();
        } 
    }
    

Solution:

As per the below conversation, the requirement was: if array( E4, E5 ,E6) already exists in database and their status is hold, and in the next turn someone selects (E3, E5, E7) (E5 being in the database and the corresponding status being hold), the page should get redirected and no value from (E3, E5, E7) array will get updated. The table will be updated only if array values are new and not on hold. So here's the solution code snippet,

if (isset($_POST['finalSeats'])) {
    $TicketType =$_POST['Tickettype'];
    $seatS=$_POST['finalSeats'];
    $EventId=$_POST["Eventid"];

    $cats = array_filter(array_map('trim', explode(',', $seatS)));
    $stmt = $con->prepare("SELECT * FROM fistevent WHERE `Event_Id`=? AND `TicketType`=? AND `seats`= ? AND status='Hold'") or die($con->error);
    $recordExists = false;
    foreach($cats as $cat ) {
        $stmt->bind_param("sss",$EventId , $TicketType,$cat);
        $stmt->execute();
        $stmt->store_result();
        if($stmt->num_rows){
            $recordExists = true;
            break;
        }
    }
    if($recordExists){
        header("Location: selection.php");
        exit();
    }else{
        foreach($cats as $cat ) {
            $_SESSION['Eventid']=$Eventid;
            $stmt = $con->prepare('UPDATE fistevent SET `Status`="Hold" where `Event_Id`=? AND `seats`= ? ') or die($con->error);
            foreach($cats as $cat ) {
                $stmt->bind_param("ss", $Eventid, $cat);
                $stmt->execute();
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

18 Comments

@rahul I've updated my answer, please see the Update(1) section of my answer. Hopefully this will resolve your issue.
sir the answer provide by RïshïKêsh Kümar is quite ok.but there is a small issue that the loop not going in if part.it goes to else part only.if you help in it you can update your answer
@rahul That's the whole point, there's no point using while() loop in your code. Have you integrated the code snippets(given in Update(1) section) in your application? Is there anything not working now?
sir your code is not working it is also not updating the records .but second answer updating the records and also check if array values matched with database values or not.. on this code only one problem that after updating the records it redirect the page as we do in if condition. otherwise it is working fine..
@rahul The else block won't redirect the user unless you have explicitly mentioned header(...);exit(); in the else part. Having said this, I've updated the else part in my answer as well. Please test your application with the updated code snippets.
|

Your Answer

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