我有这个表单,用户可以选择vehicle_type是car或motorcyle,还会根据用户选择的内容显示其他字段。
这是我的表格。基本上汽车和摩托车都有相同的属性,不同的是汽车有car_transmission和摩托车有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>
这是我的php代码
<?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>"
);
}
?>
当我尝试为Car插入数据时,我不知道为什么会出现错误,但是当我尝试为摩托车插入数据时没有问题。
这是错误
警告:oci_execute():ORA-01400:无法插入NULL (“TESTING”。“VEHICLE”。“VEHICLE_ID”)in 第212行的C:\ xampp \ htdocs \ CaRSS_ORA \ try.php
答案 0 :(得分:1)
页面上有name="vehicle_id"
的两个元素 - 当您提交页面时,PHP only keeps the value of the last one,这是摩托车的一个。当选择“car”时,摩托车vehicle_id为空,这就是你得到的。
您有几个选择:
name="vehicle_id[]"
元素,并将其用于汽车和摩托车。vehicle_id[0]
)然后引用vehicle_id[1]
(如果它是汽车)和{{1}}(如果它是摩托车)。