0

I have this form where user can choose either vehicle_type is car or motorcyle and there will be other fields that appear based on what user select.

This is my form. Basically both car and motorcycle have same attributes, the difference is car has car_transmission and motorcycle has seat_height.

<?php 
  session_start(); 
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script type="text/javascript">
        $(document).ready(function() {
            $('#vtype').on('change.states', function() {
                $("#ctype").toggle($(this).val() == 'Car');
                $("#mtype").toggle($(this).val() == 'Motorcycle');
            }).trigger('change.states');
});
  </script>
</head>
<body>

    <fieldset>
        <form action="" method="post" role="form">
            <div class="form-group">
                <label>ID</label>
                <input type="text" name="id" value="<?php echo $_SESSION['username'];?>">
            </div><br>

            <div class="form-group">
                <label>Vehicle Type</label>
                <select name="vehicle_type" id="vtype">
                    <option selected="selected" disabled="disabled">Select Vehicle Type</option>
                    <option value="Car" id="Car">Car</option>
                    <option value="Motorcycle" id="Motorcycle">Motorcycle</option>
                </select>
            </div><br>

            <div class="form-group" id="ctype">
            <div class="form-group" id="ctype">
                <label>Car Plate</label>
                <input type="text" class="form-control" placeholder="eg. MCQ1234" name="vehicle_id"></div>
                <br>

            <div class="form-group" id="ctype">
                <label>Car Brand</label>
                <select name="vehicle_brand" id="vtype">
                  <option>Select Car Brand</option>
                  <option value="BMW" id="car_brand">BMW</option>
                  <option value="Chevrolet" id="car_brand">Chevrolet</option>
                  <option value="Ford" id="car_brand">Ford</option>
                  <option value="Honda" id="car_brand">Honda</option>
                  <option value="Isuzu" id="car_brand">Isuzu</option>
                  <option value="Kia" id="car_brand">Kia</option>
                  <option value="Mazda" id="car_brand">Mazda</option>
                  <option value="Manual" id="car_brand">Mercedes</option>
                  <option value="Mitsubishi" id="car_brand">Mitsubishi</option>
                  <option value="Nissan" id="car_brand">Nissan</option>
                  <option value="Perodua" id="car_brand">Perodua</option>
                  <option value="Proton" id="car_brand">Proton</option>
                  <option value="Suzuki" id="car_brand">Suzuki</option>
                  <option value="Toyota" id="car_brand">Toyota</option>
                  <option value="Volkswagen" id="car_brand">Volkswagen</option>
                  <option value="Volvo" id="car_brand">Volvo</option>
                </select>
            </div><br>

            <div class="form-group" id="ctype">
                <label>Car Model</label>
                <input type="text" class="form-control" placeholder="" name="vehicle_model">
            </div><br>

            <div class="form-group" id="ctype">
                <label>Car Colour</label>
                <input type="text" class="form-control" placeholder="" name="vehicle_colour">
            </div><br>

            <div class="form-group" id="ctype">
                <label>Passenger Accomodation</label>
                <select name="passenger_accommodation">
                  <option selected="selected" disabled="disabled">Select Passenger Accomodation</option>
                  <option value="4">4 Passenger</option>
                  <option value="6">6 Passenger</option>
                </select>
            </div><br>

            <div class="form-group" id="ctype">
                <label>Car Transmission</label>
                <select name="car_transmission" id="car_transmission">
                  <option selected="selected" disabled="disabled">Select Car Transmission</option>
                  <option value="Automatic" id="automatic_car">Automatic</option>
                  <option value="Manual" id="manual_car">Manual</option>
                  <br>
                </select>
            </div><br>
            </div>

            <div class="form-group" id="mtype">
                <div class="form-group" id="mtype">
                <label>Motorcycle Plate</label>
                <input type="text" class="form-control" placeholder="eg. MCQ1234" name="vehicle_id"></div>
                <br>

                <div class="form-group" id="mtype">
                    <label>Motorcycle Brand</label>
                    <select name="vehicle_brand" id="vehicle_brand">
                        <option>Select Motorcycle Brand</option>
                        <option value="Ducati" id="motor_brand">Ducati</option>
                        <option value="Harley Davidson" id="motor_brand">Harley Davidson</option>
                        <option value="Honda" id="motor_brand">Honda</option>
                        <option value="Kawasaki" id="motor_brand">Kawasaki</option>
                        <option value="Nissan" id="motor_brand">Nissan</option>
                        <option value="Suzuki" id="motor_brand">Suzuki</option>
                        <option value="Yamaha" id="motor_brand">Yamaha</option>
                    </select>
                </div><br>

                <div class="form-group" id="mtype">
                <label>Motorcycle Model</label>
               <select name="vehicle_model" id="vehicle_model">
                  <option selected="selected" disabled="disabled">Select Motorcycle Model</option>
                  <option value="Cruisers" id="motor_model">Cruisers</option>
                <option value="Sport" id="motor_model">Sport</option>
                <option value="Touring" id="motor_model">Touring</option>
                <option value="Standard" id="motor_model">Standard</option>
                <option value="Dual-Sport" id="motor_model">Dual-Sport</option>
                  <br>
                </select>
            </div>
                <br>

                <div class="form-group" id="mtype">
                <label>Motorcycle Colour</label>
                <input type="text" class="form-control" placeholder="" name="vehicle_colour">
            </div>
                <br>

                <div class="form-group" id="mtype">
                <label>Passenger Accomodation</label>
                <select name="passenger_accommodation">
                  <option selected="selected" disabled="disabled">Select Passenger Accomodation</option>
                  <option value="1">1 Passenger</option>
                </select>
            </div>
                <br>

                <div class="form-group" id="mtype">
                <label>Seat Height</label>
                <select name="seat_height" id="seat_height">
                  <option selected="selected" disabled="disabled">Select Seat Height</option>
                  <option value="Low" id="automatic_car">Low</option>
                  <option value="High" id="manual_car">High</option>
                </select>
            </div><br>
            </div>

        <div class="form-group">
            <button type="submit" class="btn btn-primary" name="submit">Submit</button>
        </div>
        </form>
    </fieldset>
    <?php endif ?>
</body>
</html>

This is my php code

<?php
        // connect to the database
        $usernamedb = "testing";             // Use your username
        $password = "zaqqaz";             // and your password
        $database = "localhost/XE";   // and the connect string to connect to your database
        $db = oci_connect($usernamedb, $password, $database);

        // INSERT DATA
        if (isset($_POST['submit'])) {
        // receive all input values from the form

            $vehicle_id = $_POST['vehicle_id'];
            $driverid = $_POST['id'];
            $vehicle_type = $_POST['vehicle_type'];
            $vehicle_brand = $_POST['vehicle_brand'];
            $vehicle_model = $_POST['vehicle_model'];
            $vehicle_colour = $_POST['vehicle_colour'];
            $passenger_accommodation = $_POST['passenger_accommodation'];

             $query = "INSERT INTO VEHICLE (VEHICLE_ID, DRIVER_ID, VEHICLE_TYPE, VEHICLE_BRAND, VEHICLE_MODEL, VEHICLE_COLOUR, PASSENGER_ACCOMMODATION)
                VALUES(:vehicle_id, :driverid, :vehicle_type, :vehicle_brand, :vehicle_model, :vehicle_colour, :passenger_accommodation)";

                $compile = oci_parse($db, $query);

                oci_bind_by_name($compile, ':vehicle_id', $vehicle_id);
                oci_bind_by_name($compile, ':driverid', $driverid);
                oci_bind_by_name($compile, ':vehicle_type', $vehicle_type);
                oci_bind_by_name($compile, ':vehicle_brand', $vehicle_brand);
                oci_bind_by_name($compile, ':vehicle_model', $vehicle_model);
                oci_bind_by_name($compile, ':vehicle_colour', $vehicle_colour);
                oci_bind_by_name($compile, ':passenger_accommodation', $passenger_accommodation);

                oci_execute($compile);

                if($vehicle_type == 'Car') {
                    $car_transmission = $_POST['car_transmission'];

                    $query = "UPDATE VEHICLE SET CAR_TRANSMISSION='$car_transmission' WHERE DRIVER_ID='$driverid'";

                    $compile = oci_parse($db, $query);

                    oci_execute($compile);
                }
                if($vehicle_type == 'Motorcycle') {
                    $seat_height = $_POST['seat_height'];

                    $query = "UPDATE VEHICLE SET SEAT_HEIGHT='$seat_height' WHERE DRIVER_ID='$driverid'";

                    $compile = oci_parse($db, $query);

                    oci_execute($compile);
                }

                 echo
                 (
                    "<SCRIPT LANGUAGE='JavaScript'>
                    window.alert('Insert Succesfull!')
                    </SCRIPT>"
                 );
             }
             ?>

I don't know why there is error when I try to insert data for Car but there is no problem when I try to insert data for motorcycle.

This is the error

Warning: oci_execute(): ORA-01400: cannot insert NULL into ("TESTING"."VEHICLE"."VEHICLE_ID") in C:\xampp\htdocs\CaRSS_ORA\try.php on line 212

Rhya
  • 47
  • 1
  • 12
  • 2
    The error seems pretty clear. You are trying to insert a null value for the `vehicle_id`. Dump the `$_POST` see if it comes over as expected. You are also vulnerable to SQL injection and should address that. – ficuscr May 29 '18 at 16:26

1 Answers1

1

You have two elements with name="vehicle_id" on the page - when you submit the page, PHP only keeps the value of the last one, which is the motorcycle one. When "car" is selected, the motorcycle vehicle_id is null, so that's what you get.

You have a few options:

  • you could disable the form fields which aren't being used, depending on whether "car" or "motorcycle" was selected. This would prevent the motorcycle fields from being submitted when "car" was selected (hiding them isn't enough).
  • you could just have one name="vehicle_id" element on the page, and use it for both cars and motorcycles.
  • you could make all of your names into arrays (e.g. name="vehicle_id[]") and then reference vehicle_id[0] if it's a car and vehicle_id[1] if it's a motorcycle.
  • probably there's other possibilities, but those were the first ones I thought of.
kfinity
  • 8,581
  • 1
  • 13
  • 20
  • How do I disable the hidden fields? – Rhya May 30 '18 at 03:38
  • You add a `disabled` property to the field element. It looks like you're using jquery, so here's how to do it that way: https://learn.jquery.com/using-jquery-core/faq/how-do-i-disable-enable-a-form-element/ – kfinity May 30 '18 at 13:44