Need help with if else condition?

if($triptype == 'outstation') {          
    if(isset($_POST['oneway_travel_det1'])) {
        $travelDet = parseValues($_POST['oneway_travel_det1']);
        foreach ($travelDet as $det) {
            if(isset($det['to_airport'])) {
                $toAirport = $det['to_airport'];
            }
            else {
                $toAirport = 0;
            }

            if(isset($det['from_airport'])) {
                $fromAirport = $det['from_airport'];
            }
            else {
                $fromAirport = 0;
            }

            $amt = (!empty($det['est_cost'])) ? $det['est_cost'] : 0;
            $dep_place = $arr_place = '';
            $dep_place = (!empty($det['dep_ocity1'])) ? $det['dep_ocity1'] : $det['dep_oplace1']; //get entered city for international
            $arr_place = (!empty($det['arr_ocity1'])) ? $det['arr_ocity1'] : $det['arr_oplace1']; //get entered city for international

            //insert query
            $query = "INSERT INTO `tools_taxi`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_oplace1'] . "','" . $arr_place . "','" . $det['arr_oplace1'] . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."')";

            echo "INSERT INTO `tools_taxi`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_oplace1'] . "','" . $arr_place . "','" . $det['arr_oplace1'] . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."')";

            $res = mysqli_query($taxi_conn, $query);
                if (!$res) {
                    die('bta_travel_itenary table insert failed' . mysqli_error($taxi_conn));  
                    trace_log(__FILE__, __LINE__, $taxi_id . " : " . $query . mysqli_error($taxi_conn));                 
                }
                else{
                    trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Sucessfully.");
                }
            }
        }
    // }

    else {
        $travelDet = parseValues($_POST['round_travel_det1']);
        foreach ($travelDet as $det) {            
            $amt = (!empty($det['est_cost'])) ? $det['est_cost'] : 0;
            $dep_place = $arr_place = '';
            $dep_place = (!empty($det['dep_rcity1'])) ? $det['dep_rcity1'] : $det['dep_rplace1']; //get entered city for international
            $arr_place = (!empty($det['arr_rcity1'])) ? $det['arr_rcity1'] : $det['arr_rplace1']; //get entered city for international
            //insert query
            $query = "INSERT INTO `tools_taxi`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_rplace1'] . "','" . $arr_place . "','" . $det['arr_rplace1'] . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."')";

                echo "INSERT INTO `tools_taxi`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_rplace1'] . "','" . $arr_place . "','" . $det['arr_rplace1'] . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."')";

            $res = mysqli_query($taxi_conn, $query);
                if (!$res) {
                    die('trip_itenary table insert failed' . mysqli_error($taxi_conn));  
                    trace_log(__FILE__, __LINE__, $taxi_id . " : " . $query . mysqli_error($taxi_conn));                 
                }
                else{
                    trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Sucessfully.");
                }
            }
        }
    }
// 
elseif($triptype == 'city') {
    if (isset($_POST['oneway_travel_det'])) {
        /*One way travel*/
        $travelDet = parseValues($_POST['oneway_travel_det']);
        foreach ($travelDet as $det) {            
            $amt = (!empty($det['est_cost'])) ? $det['est_cost'] : 0;
            $dep_place = $arr_place = '';
            $dep_place = (!empty($det['dep_ocity'])) ? $det['dep_ocity'] : $det['dep_oplace']; //get entered city for international
            $arr_place = (!empty($det['arr_ocity'])) ? $det['arr_ocity'] : null; //get entered city for international
            //insert query
            $query = "INSERT INTO `tools_taxi`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`dept_date`,`dept_time`, `trip_type`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_oplace'] . "','" . $arr_place . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $_POST['time_zone3'] . "', '".$type_of_trip."')";

            echo "INSERT INTO `tools_taxi`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`dept_date`,`dept_time`, `trip_type`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_oplace'] . "','" . $arr_place . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $_POST['time_zone3'] . "', '".$type_of_trip."')";

            $res = mysqli_query($taxi_conn, $query);
                if (!$res) {
                    die('bta_travel_itenary table insert failed' . mysqli_error($taxi_conn));  
                    trace_log(__FILE__, __LINE__, $taxi_id . " : " . $query . mysqli_error($taxi_conn));                 
                }
                else {
                    trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Sucessfully.");
                }
            }
        }
        }
    else {
        if (isset($_POST['round_travel_det'])) {
        /*Round Trip Travel*/
        $travelDet = parseValues($_POST['round_travel_det']);
        foreach ($travelDet as $det) {            
            $amt = (!empty($det['est_cost'])) ? $det['est_cost'] : 0;
            $dep_place = $arr_place = '';
            $dep_place = (!empty($det['dep_rcity'])) ? $det['dep_rcity'] : $det['dep_rplace']; //get entered city for international
            $arr_place = (!empty($det['arr_rcity'])) ? $det['arr_rcity'] : null; //get entered city for international
            //insert query
            $query = "INSERT INTO `tools_taxi`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`dept_date`,`dept_time`, `trip_type`) VALUES
                            ('".$taxi_id."','" . $dep_place . "','" . $det['dep_rplace'] . "','" . $arr_place . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."')";

            echo "INSERT INTO `tools_taxi`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`dept_date`,`dept_time`, `trip_type`) VALUES
                            ('".$taxi_id."','" . $dep_place . "','" . $det['dep_rplace'] . "','" . $arr_place . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."')";

            $res = mysqli_query($taxi_conn, $query);
                if (!$res) {
                    die('trip_itenary table insert failed' . mysqli_error($taxi_conn));  
                    trace_log(__FILE__, __LINE__, $taxi_id . " : " . $query . mysqli_error($taxi_conn));                 
                }
                else{
                    trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Sucessfully.");
                }
            }
        }
    }

As shown in the above code I am trying to insert the values in the database. The problem here is that when I try to insert values wrt “outstation” condition, only the if condition is executing i.e., $_POST[‘oneway_travel_det1’] and not the $_POST[‘round_travel_det1’]. When I enter data wrt round trip the details are not getting inserted. Please let me know if there is anything wrong.

Your code has 4 choices -

  1. outstation - one way
  2. outstation - round trip
  3. city - one way
  4. city - round trip

I recommend that you indent and add comments to your code so that you can identify the starting and ending point for each of those choices. You currently have an } else { that should be for the city - round trip choice, but it is a point in the logic for a third choice after the outstation and city logic. It is not at the point in the logic for the city - round trip choice.

Part of the problem is you have too much repetitive code. Some suggestions -

  1. If there are only two choices - outstation and city, use an if(…) {…} else {…}. You currently have an if(…) {…} elseif(…) {…} else {…} with the last else {…} being where the mistake is in the logic.
  2. The only thing that should be inside the conditional logic is the code that is different between the choices. You should set up common variables inside the conditional logic, then have only one unique instance of each different INSERT query, that gets executed using the common variables as the input data.
  3. Because the only apparent difference between the two unique INSERT queries is one column, wouldn’t it be easier to set up a common variable in the conditional logic, with a null value for that field, then you could use one single query.
  4. You should use prepared queries, prepared once, before the start of the looping. This will make the queries safe and provide a speed improvement in the execution of the queries. You probably should switch to the much simpler PDO extension, since using prepared queries with the mysqli extension is overly complicated.
  5. While the echo statements for the sql queries are just there for debugging, wouldn’t echo $query; be simpler?
  6. If you use exceptions for handling database statement errors, you can eliminate the error handling logic you have now. Your main code will only ‘see’ error free execution since an error will cause execution to transfer to the exception handling.

In looking at the code further, there are isset() tests for -

$_POST['oneway_travel_det1']
$_POST['oneway_travel_det']
$_POST['round_travel_det']

but there is not one for -

$_POST['round_travel_det1']

The logic assumes that if there is not a ‘oneway_travel_det1’, it must be a ‘round_travel_det1’, but this may not be the case (we don’t know what your submitted data actually looks like.)

This inconstancy may have contributed to the mistake in the } else { logic mentioned above and may have something to do with the incorrect operation you are asking about.

In any case, after you fix the mistakes in the conditional logic, you need to examine what the submitted data actually is (see print_r() or var_dump()) in order to find what’s causing the problem.

I will check with the code and get back to you all. Based on the suggestions i will re-check my code and see.

@phdr Hi. I went through the code and tried changing but unable to insert. The workflow is like:

  1. There are 2 choices namely, Outstation and City Local.
  2. Now when i select Outstation there are further more 2 choices namely round trip and one way. As of now only the one way details are getting inserted for Outstation logic and not the round trip as shown in the above code.
  3. For City local, the logic is same as explained in step 2 but in this for both round trip and one way details are getting inserted properly.
    So can you please help me furthermore?

I need to review the code more, but I am seeing a lot of complication with duplicated code.

To abstract this out, list off what should happen for each combination. What changes?

The following is what your code should end up looking like, with conditional logic only for those things that are different between the choices, and with the other suggestions that have already been made, including using the much simpler PDO extension due to the prepared query -

<?php

// if the 4 values - $_POST['oneway_travel_det1'], $_POST['round_travel_det1'], $_POST['oneway_travel_det'], and $_POST['round_travel_det']
// are being set properly in the form, only one will (should) be set at a time and will uniquely identify which one of the 4 choices to use

// the following reduced code should (untested) work -
if(isset($_POST['oneway_travel_det1']))
{
	// outstation - one way
	$travelDet = parseValues($_POST['oneway_travel_det1']);
	$city = 'ocity1';
	$place = 'oplace1';
	$arr_place = true; // true - use the arr_place for the country. false - use null for the country.
}

if(isset($_POST['round_travel_det1']))
{
	// outstation - round trip
	$travelDet = parseValues($_POST['round_travel_det1']);
	$city = 'rcity1';
	$place = 'rplace1';
	$arr_place = true; // true - use the arr_place for the country. false - use null for the country.
}

if(isset($_POST['oneway_travel_det']))
{
	// city - one way
	$travelDet = parseValues($_POST['oneway_travel_det']);
	$city = 'ocity';
	$place = 'oplace';
	$arr_place = false; // true - use the arr_place for the country. false - use null for the country.
}

// edit - the following line had a typo error in it that has since been corrected
if(isset($_POST['round_travel_det']))
{
	// city - round trip
	$travelDet = parseValues($_POST['round_travel_det']);
	$city = 'rcity';
	$place = 'rplace';
	$arr_place = false; // true - use the arr_place for the country. false - use null for the country.
}

// if the code up to this point can only be executed if there IS a trip, no additional conditional logic is needed
// however, if the code up to this point can get executed without one of these 4 variables being set, one more conditional statement will be needed around the following code

// prepare the query once
$query = "INSERT INTO `tools_taxi`.`trip_itenary`
	(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`)
	VALUES
	(?,?,?,?,?,?,?,?)";
	
// this example uses the much simpler PDO extension and will not work AS IS if all you do is copy/paste it
$stmt = $pdo->prepare($query);

foreach ($travelDet as $det)
{
	// if there's no arrival place (country), use null
	if(!$arr_place){
		$det["arr_$place"] = null;
	}
	
	$dep_place = !empty($det["dep_$city"]) ? $det["dep_$city"] : $det["dep_$place"]; //get entered city for international
	$arr_place = !empty($det["arr_$city"]) ? $det["arr_$city"] : $det["arr_$place"]; //get entered city for international
	$dep_conutry = $det["dep_$place"];
	$arr_country = $det["arr_$place"];

	// execute the query
	$stmt->execute([
		$taxi_id,
		$dep_place,
		$dep_country,
		$arr_place,
		$arr_country,
		date('Y-m-d', strtotime($det['dep_date'])),
		$det['time_zone3'],
		$type_of_trip
	]);

	trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Successfully.");
}

If one of the choices doesn’t get executed, either in your code or in the above, it is likely because the value being tested isn’t set, and you would need to do something else that has already been suggested and examine the submitted data to find what is causing the problem.

1 Like

Sorry for late reply. I will try it and let you know, thank you so much.

This is the error that I am getting after solving the way you asked me to do.

Hi All,

I tried solving it but facing the issues. No error but the data insertion is not happening for one of the values. So can I make use of swtich statement to solve the bug?

I request you to guide me as I am still not getting any solution for this.

Hi All,
I have been trying to use all ways to solve this but each time it is giving me some issue or the other. I have as of now tried using if … else condition but still the data insertion is not correct. Can anyone please guide me? It’s a request

if($triptype == 'outstation') {
        /*Round Trip Data for Oustation*/
        $travelDet = parseValues($_POST['round_travel_det1']);
        foreach ($travelDet as $det) {            
            $amt = (!empty($det['est_cost'])) ? $det['est_cost'] : 0;
            $dep_place = $arr_place = '';
            $dep_place = (!empty($det['dep_rcity1'])) ? $det['dep_rcity1'] : $det['dep_rplace1']; //get entered city for international
            $arr_place = (!empty($det['arr_rcity1'])) ? $det['arr_rcity1'] : $det['arr_rplace1']; //get entered city for international
            $type_of_trip = (!empty($det['type_of_trip'])) ? $det['type_of_trip'] : null;
            //insert query
            $query = "INSERT INTO `tools_bta`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`, `payment_mode`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_rplace1'] . "','" . $arr_place . "','" . $det['arr_rcity1'] . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."', '".$payment_mode."')";

            echo "Shruti321";
                echo "INSERT INTO `tools_bta`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`, `payment_mode`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_rplace1'] . "','" . $arr_place . "','" . $det['arr_rcity1'] . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."', '".$payment_mode."')";

            $res = mysqli_query($taxi_conn, $query);
                if (!$res) {
                    die('trip_itenary table insert failed321' . mysqli_error($taxi_conn));  
                    trace_log(__FILE__, __LINE__, $taxi_id . " : " . $query . mysqli_error($taxi_conn));                 
                }
                else{
                    trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Sucessfully321.");
                }
            }
        } 
        else {
            /*OneWay Trip Data for Oustation*/
            $travelDet = parseValues($_POST['oneway_travel_det1']);
            foreach ($travelDet as $det) {           

                $amt = (!empty($det['est_cost'])) ? $det['est_cost'] : 0;
                $dep_place = $arr_place = '';
                $dep_place = (!empty($det['dep_ocity1'])) ? $det['dep_ocity1'] : $det['dep_oplace1']; //get entered city for international
                $arr_place = (!empty($det['arr_ocity1'])) ? $det['arr_ocity1'] : $det['arr_oplace1']; //get entered city for international
                $type_of_trip = (!empty($det['type_of_trip'])) ? $det['type_of_trip'] : null;

                //insert query $bill_to_comp $cust_pay $cash
                $query = "INSERT INTO `tools_bta`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`, `payment_mode`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_oplace1'] . "','" . $arr_place . "','" . $det['arr_oplace1'] . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."', '".$payment_mode."')";
                echo "Shruti123";
                echo "INSERT INTO `tools_bta`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`arrival_country`,`dept_date`,`dept_time`, `trip_type`, `payment_mode`)  VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_oplace1'] . "','" . $arr_place . "','" . $det['arr_oplace1'] . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."', '".$payment_mode."')";

                $res = mysqli_query($taxi_conn, $query);
                    if (!$res) {
                        die('bta_travel_itenary table insert failed123' . mysqli_error($taxi_conn));  
                        trace_log(__FILE__, __LINE__, $taxi_id . " : " . $query . mysqli_error($taxi_conn));                 
                    }
                    else{
                        trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Sucessfully123.");
                    }
                }
            
            }

        if($triptype == 'city') {
            /*Round Trip Data for City Local*/
            $travelDet = parseValues($_POST['round_travel_det']);
            foreach ($travelDet as $det) {            
                $amt = (!empty($det['est_cost'])) ? $det['est_cost'] : 0;
                $dep_place = $arr_place = '';
                $dep_place = (!empty($det['dep_rcity'])) ? $det['dep_rcity'] : $det['dep_rplace']; //get entered city for international
                $arr_place = (!empty($det['arr_rcity'])) ? $det['arr_rcity'] : null; //get entered city for international
                $type_of_trip = (!empty($det['type_of_trip'])) ? $det['type_of_trip'] : null;
                //insert query
                $query = "INSERT INTO `tools_bta`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`dept_date`,`dept_time`, `trip_type`, `payment_mode`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_rplace'] . "','" . $arr_place . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."', '".$payment_mode."')";

                echo "Shruti654";
                echo "INSERT INTO `tools_bta`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`dept_date`,`dept_time`, `trip_type`, `payment_mode`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_rplace'] . "','" . $arr_place . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."', '".$payment_mode."')";

                $res = mysqli_query($taxi_conn, $query);
                    if (!$res) {
                        die('trip_itenary table insert failed654' . mysqli_error($taxi_conn));  
                        trace_log(__FILE__, __LINE__, $taxi_id . " : " . $query . mysqli_error($taxi_conn));                 
                    }
                    else{
                        trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Sucessfully654.");
                    }
                }
            }
            else {
                /*Oneway Trip Data for City Local*/
                $travelDet = parseValues($_POST['oneway_travel_det']);
        foreach ($travelDet as $det) {            
            $amt = (!empty($det['est_cost'])) ? $det['est_cost'] : 0;
            $dep_place = $arr_place = '';
            $dep_place = (!empty($det['dep_ocity'])) ? $det['dep_ocity'] : $det['dep_oplace']; //get entered city for international
            $arr_place = (!empty($det['arr_ocity'])) ? $det['arr_ocity'] : null; //get entered city for international
            $type_of_trip = (!empty($det['type_of_trip'])) ? $det['type_of_trip'] : null;
            //insert query
            $query = "INSERT INTO `tools_bta`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`dept_date`,`dept_time`, `trip_type`, `payment_mode`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_oplace'] . "','" . $arr_place . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "', '".$type_of_trip."', '".$payment_mode."')";

            echo "Shruti456";
            echo "INSERT INTO `tools_bta`.`trip_itenary`(`trip_id`,`departure`,`departure_country`,`arrival`,`dept_date`,`dept_time`, `trip_type`, `payment_mode`) VALUES ('".$taxi_id."','" . $dep_place . "','" . $det['dep_oplace'] . "','" . $arr_place . "','" . date('Y-m-d', strtotime($det['dep_date'])) . "','" . $det['time_zone3'] . "','".$type_of_trip."', '".$payment_mode."')";

            $res = mysqli_query($taxi_conn, $query);
                if (!$res) {
                    die('bta_travel_itenary table insert failed456' . mysqli_error($taxi_conn));  
                    trace_log(__FILE__, __LINE__, $taxi_id . " : " . $query . mysqli_error($taxi_conn));                 
                }
                else {
                    trace_log(__FILE__, __LINE__, $taxi_id . " : History Updated Sucessfully456.");
                }
            }
            }

Have you even defined/listed, as a comment in your code, what the inputs and their values are, for each of the four total combinations of trip type (outstation/city) and round/oneway trip? Without a clear definition of what the inputs are, it will be impossible to write code that does what you want.

Your current logic is testing $triptype, twice -

if($triptype == 'outstation'){
	// code #1 - uses $_POST['round_travel_det1'] - outstation round trip data
}else{
	// code #2 - uses $_POST['oneway_travel_det1'] - outstation one way trip data
}

if($triptype == 'city'){
	// code #3 - uses $_POST['round_travel_det'] - city round trip data
}else{
	// code #4 - uses $_POST['oneway_travel_det'] - city one way trip data
}

Assuming $triptype can only be ‘outstation’ or ‘city’, the above logic will execute code #1 (outstation round trip) and #4 (city one way) when $triptype is ‘outstation’ and it will execute code #2 (outstation one way) and #3 (city round trip) when $triptype is ‘city’. Is that what you want to happen? Does that even make sense?

Sorry for not defining it. But the code in the first loop is for outstation within that is the trip data i.e., whether it is round trip or one way trip. Similarly for City the condition will be the same.
As of now only the round travel details are getting inserted but when i try to enter the one way related data it is inserting null/defaults values which is set in round trip.@phdr

Yes. Based on the tab selection the data must get inserted into the database.

As you can see in the image there are two bootstrap tabs for the dropdown as “Outstation”. So when I select either round trip or one way it’s resp data must get inserted into the table.

My statement isn’t for you to (re)state what you are trying to accomplish (I/we already know that), it is for you to literally list what the input variables are and for each value they can have, what processing you are going to do. Once you define this literal information, you can post it so that we won’t be guessing what values should be tested/used in the code. The next step will be to check, using either print_r() or var_dump(), if you are actually receiving the expected data for each combination of type of trip and one way/round trip travel.

<tr>
                                                        <td>
                                                            <select id='Select' name="trip_type">
                                                                <option value='outstation' name ="outstation">Outstation</option>
                                                                <option value='city' name ="city">City Local</option>
                                                            </select>
                                                            <br><br>
                                                            <div class="tab-content">
                                                                <div class="tab-pane active" id="outstation">
                                                                	<!-- Home content -->
                                                                    <ul>
                                                                        <li class="active">
                                                                            <a href="#roundtrip_tbl1" name="roundtrip_tbl1">Round Trip</a>
                                                                        </li>
                                                                        <li>
                                                                            <a href="#oneway_tbl1" name="oneway_tbl1">One Way</a>
                                                                        </li>
                                                                    </ul>

                                                            <div id="roundtrip_tbl1" style="width: 25vw;">
                                                                <table border="1" bordercolor="#c1dde4" cellspacing="0" cellpadding="5" id="roundtrip_tbl_travel1" >
                                                                    <tr class="accombg" >
                                                                        <td colspan="16" align="center" ><strong>Round City Local Travel --</strong><strong>Date and Time </strong></td>
                                                                    </tr>

                                                                    <tr class="accombg" >
                                                                        <td align="center">Origin Location <span class="redtext">* </span ></td>
                                                                        <td align="center">City Pick Up<span class="redtext">*</span><p><span class="accombgsmalltext"> (Pick up Point)</span></p></td>
                                                                        <td style="min-width:50px;max-width:50px" align="center">Pick up Date<span class="redtext">*</span></td>
                                                                        <td width="60" align="center">Pick Up Time</td>
                                                                        <td align="center">Destination Loc <span class="redtext">*</span><p><span class="accombgsmalltext"> </span></p></td>                        
                                                                        <td align="center">Drop Point <span class="redtext">*</span><p><span class="accombgsmalltext"> </span></p></td>
                                                                    <tr class="sectionbg" id="jsTravelRoundDet1">
                                                                        <td>
                                                                            <div>   
                                                                                <input type="text" size="10" name="round_travel_det1[dep_rplace1][]" id="dep_rplace" readonly="readonly" value="<?php echo $emp_det['off_location']; ?>" class="jsTravelCls jsCityCls"/>
                                                                            </div>
                                                                        </td>

                                                                        <td>
                                                                            <textarea maxlength="20" minlength="5" name="round_travel_det1[dep_rcity1][]" id="dep_rcity" class="jsTravelCls" ></textarea>
                                                                          
                                                                        </td>
                                                                        <script type="text/javascript">
                                                                            $('.assigned_date').datepicker({
                                                                                format: 'mm/dd/yyyy',
                                                                                minDate: new Date(), // = today
                                                                            });
                                                                        </script>
                                                                        <td>
                                                                            <input style="min-width:80px;max-width:80px" type="text" name="round_travel_det1[dep_date][]" value="" id="assigned_date" class="assigned_date" />
                                                                        </td>

                                                                        <td>
                                                                            <div>
                                                                                <select name="round_travel_det1[time_zone3][]" style="max-width:50px"  class="jsTravelCls">
                                                                                    <?php
                                                                                        foreach ($time_zone as $tme_zone) {
                                                                                            echo "<option value=\"" . $tme_zone . "\">" . $tme_zone . "</option>";
                                                                                        }
                                                                                    ?>
                                                                                </select>
                                                                            </div>
                                                                        </td>

                                                                        <td>
                                                                            <div>
                                                                                <select name="round_travel_det1[arr_rplace1][]" style="width:110px;" onchange="showForm19(this.value, this, 'arr_rplace1');" id="arr_place" class="jsTravelCls jsCityCls">
                                                                                    <?php foreach ($city_list as $key => $value) { ?>
                                                                                        <?php if($label == '' || $value['group_name'] != $label) { ?>
                                                                                            <?php if($group) { ?>
                                                                                                </optgroup>
                                                                                            <?php } ?>
                                                                                            <optgroup label="<?php echo ($value['group_name'] == 'INDIA') ? 'DOMESTIC': $value['group_name']; ?>" class="grey">
                                                                                        <?php
                                                                                                $group ++;
                                                                                                $label = $value['group_name'];
                                                                                            } ?>
                                                                                            <option <?php echo (in_array($value['name'], $city_sens)) ? "style='color:red'" : ''; ?> value="<?php echo $key ?>" ><?php echo $value['name'] ?></option>
                                                                                            <?php } ?> 
                                                                                            <optgroup label="Others">
                                                                                                 <option value="Others">Others</option>
                                                                                            </optgroup>
                                                                                    </select>
                                                                                </div>
                                                                        </td>

                                                                        <td>
                                                                            <textarea name="round_travel_det1[arr_rcity1][]" id="arr_rcity" class="jsTravelCls"></textarea>
                                                                        </td>

                                                                            <div align="left">
                                                                                <input type="hidden" name="round_travel_det1[type_of_trip][]" id="type_of_trip" style="width:80px;"  class="jsTravelCls" value="0"></input>
                                                                            </div>
                                                                    </tr>
                                                                </table>

                                                                <table>
                                                                    <tr>
                                                                        <td>
                                                                            <p id="addnew">
                                                                                <a href="javascript:void(0)" class="addlinks" onclick="addTravelRow_2()"> + Insert Row </a> 
                                                                            </p>
                                                                        </td>

                                                                        <td>
                                                                            <p id="delrow_t" >
                                                                                <a href="javascript:void(0)" class="addlinks" onclick="delIt('roundtrip_tbl_travel1')"> - Delete Row </a>
                                                                            </p>
                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                            </div> 

                                                            <div id="oneway_tbl1">
                                                                <table style="max-width:880px" border="1" bordercolor="#c1dde4" cellspacing="0" cellpadding="5" id="oneway_tbl_travel1">
                                                                    <tr class="accombg">
                                                                        <td colspan="4" align="center"><strong>One Way Travel</strong></td>
                                                                        <td colspan="4" align="center"><strong>Date and Time </strong></td>
                                                                    </tr>
                                                                    
                                                                    <tr class="accombg">
                                                                        <td align="center">Origin Location <span class="redtext">* </span ></td>
                                                                        <td align="center">City Pick Up<span class="redtext">*</span><p><span class="accombgsmalltext"> (Pick up Point)</span></p></td>
                                                                        <td style="min-width:50px;max-width:50px" align="center">Pick up Date<span class="redtext">*</span></td>
                                                                        <td width="60" align="center">Pick Up Time</td>
                                                                        <td align="center">Drop Off Loc <span class="redtext">*</span><p><span class="accombgsmalltext"> </span></p></td>                        
                                                                        <td align="center">Drop Point <span class="redtext">*</span><p><span class="accombgsmalltext"> </span></p></td>
                                                                    </tr>

                                                                    <tr class="sectionbg" id="jsTravelOneWayDet">
                                                                        <td>
                                                                            <div>
                                                                                <input type="text" size="10" name="oneway_travel_det1[dep_oplace1][]" id="dep_place" readonly="readonly" value="<?php echo $emp_det['off_location']; ?>" class="jsTravelCls jsCityCls"/>
                                                                            </div>
                                                                        </td>

                                                                        <td>
                                                                            <textarea name="oneway_travel_det1[dep_rcity1][]" id="dep_city"  class="jsTravelCls" ></textarea>>
                                                                        </td>
                                                                        <script type="text/javascript">
                                                                            $('.last_date').datepicker({
                                                                                format: 'mm/dd/yyyy',
                                                                                minDate: new Date(), // = today
                                                                            });
                                                                        </script>
                                                                        <td>
                                                                            <input style="min-width:80px;max-width:80px" type="text" name="oneway_travel_det1[dep_date][]" value="" id="last_date" class="assigned_date" />
                                                                        </td>

                                                                        <td>
                                                                            <div>
                                                                                <select name="oneway_travel_det1[time_zone3][]" style="max-width:50px"  class="jsTravelCls"> 
                                                                                    <?php
                                                                                        foreach ($time_zone as $tme_zone) {
                                                                                            echo "<option value=\"" . $tme_zone . "\">" . $tme_zone . "</option>";
                                                                                        }
                                                                                    ?>
                                                                                </select>
                                                                            </div>
                                                                        </td>

                                                                        <td>
                                                                            <div>
                                                                                <select name="oneway_travel_det1[arr_rplace1][]" style="width:110px;" onchange="showForm19(this.value, this, 'arr_rplace1');" id="arr_place" class="jsTravelCls jsCityCls">
                                                                                    <?php foreach ($city_list as $key => $value) { ?>
                                                                                        <?php if($label == '' || $value['group_name'] != $label) { ?>
                                                                                            <?php if($group) { ?>
                                                                                                </optgroup>
                                                                                            <?php } ?>
                                                                                            <optgroup label="<?php echo ($value['group_name'] == 'INDIA') ? 'DOMESTIC': $value['group_name']; ?>" class="grey">
                                                                                        <?php
                                                                                            $group ++;
                                                                                            $label = $value['group_name'];
                                                                                        } ?>
                                                                                        <option <?php echo (in_array($value['name'], $city_sens)) ? "style='color:red'" : ''; ?> value="<?php echo $key ?>" ><?php echo $value['name'] ?></option>
                                                                                        <?php } ?> 
                                                                                        <optgroup label="Others">
                                                                                             <option value="Others">Others</option>
                                                                                        </optgroup>
                                                                                </select>
                                                                            </div>
                                                                        </td>

                                                                        <td>
                                                                            <textarea name="oneway_travel_det1[arr_rcity1][]" id="arr_city"  class="jsTravelCls" ></textarea>
                                                                            
                                                                        </td>

                                                                   
                                                                        <div align="left">
                                                                            <input type="hidden" name="oneway_travel_det1[type_of_trip][]" id="type_of_trip" style="width:80px;"  class="jsTravelCls" value="1"></input>
                                                                        </div>
                                                                  
                                                                    </tr>
                                                                </table>
                                                            </div>
                                                        </div>

                                                        <tr>
                                                            <td>&nbsp;</td>
                                                        </tr>

I added bbcode code tags [code][/code] around your code so that the forum software would display it as is. Please do this yourself in the future when posting code.

Okay sure I will keep that in mind. Above code is the html form as to how the variables are stored in the table.I am using those variables and calling them in the query. Please let me know whether it is correct or not?

Sponsor our Newsletter | Privacy Policy | Terms of Service