使用选择下拉列表的主键作为新表中的外键

时间:2017-03-21 02:23:45

标签: php mysql

我尝试将在下拉列表中选择的主键作为新表的外键插入。我正在使用php和mysql。

我一直在notice:undefined index。我已经创建了3个表(类型品牌模型)模型表,包含来自类型和品牌模型的外键。

我用来插入数据的HTML / Php:

<div class="modal fade" id="addVehicleModel" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="panel panel-danger">
        <div class="panel-heading">
          <h4>Add New Vehicle Model</h4>
        </div>
        <div class="panel-body">
          <form role="form-horizontal" action="addVehicleModel.php" method="post">
            <div class="form-group">
              <label>Vehicle Model:</label>
              <input class="form-control" type="text" id="vehicleModel" name="vehicleModel" placeholder="Enter New Vehicle Model" />
            </div>
            <div class="form-group">
              <label for="vehicleType">Vehicle Type:</label>
              <div>
                <select class="form-control" onchange="change_vehicleType()" name="vehicleType" id="vehicleTypeID">
                  <option value="">Select Vehicle Type</option>
                  <?php
                  $res=mysqli_query($link,"Select * from vehicleType");
                  while ($row=mysqli_fetch_array($res)) { ?>
                    <option value=<?php echo $row[ 'id_vehicleType'];?>>
                      <?php echo $row['vehicle_Type'];?>
                    </option>
                    <?php
                    } ?>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label for="vehicleType">Vehicle Brand:</label>
              <div>
                <select class="form-control" onchange="change_vehicleBrand()" name="vehicleBrand" id="vehicleBrandID">
                  <option value="">Select Vehicle Brand</option>
                  <?php
                  $res=mysqli_query($link,"Select * from vehicleBrand");
                  while ($row=mysqli_fetch_array($res)) { ?>
                    <option value="<?php echo $row['id_vehicleBrand'];?>">
                      <?php echo $row['vehicle_Brand'];?>
                    </option>
                    <?php
                    } ?>
                </select>
              </div>
            </div>
            <br>
            <button type="submit" class="btn btn-danger">Submit</button>
            <button type="submit" class="btn btn-primary" data-dismiss="modal">Close</button>
          </form>
        </div>
        <!--class panel body end-->
      </div>
      <!--class panel danger end-->
    </div>
    <!--class modal content end-->
  </div>
  <!--class modal dialog end-->
</div>
<!--class modal ADD VEHICLE MODEL end-->

我使用的Php动作:

<?php
$mysqli = new mysqli("localhost", "root", "root", "vms");
if ($mysqli === false) {
    die("ERROR: Could not connect. " . $mysqli->connect_error);
}
$vehicleModel   = $mysqli->real_escape_string($_REQUEST['vehicleModel']);
$vehicleTypeID  = $mysqli->real_escape_string($_REQUEST['id_vehicleType']);
$vehicleBrandID = $mysqli->real_escape_string($_REQUEST['id_vehicleBrand']);
$sql            = "INSERT INTO vehiclemodel (vehicle_Model, id_FKvehicleType,        id_FKvehicleBrand) VALUES ('$vehicleModel', '$id_vehicleType', '$id_vehicleBrand')";
if ($mysqli->query($sql) === true) {
    echo "Records inserted successfully.";
} else {
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
$mysqli->close();
?>

我得到的错误

Notice: Undefined index: id_vehicleType in
C:\xampp\htdocs\vms\addVehicleModel.php on line 13

Notice: Undefined index: id_vehicleBrand in
C:\xampp\htdocs\vms\addVehicleModel.php on line 14

Notice: Undefined variable: id_vehicleType in
C:\xampp\htdocs\vms\addVehicleModel.php on line 17

Notice: Undefined variable: id_vehicleBrand in
C:\xampp\htdocs\vms\addVehicleModel.php on line 17 ERROR: Could not
able to execute INSERT INTO vehiclemodel (vehicle_Model,
id_FKvehicleType, id_FKvehicleBrand) VALUES ('Saga', '', ''). Cannot
add or update a child row: a foreign key constraint fails
(`vms`.`vehiclemodel`, CONSTRAINT `vehiclemodel_ibfk_1` FOREIGN KEY
(`id_FKvehicleType`) REFERENCES `vehicletype` (`id_vehicleType`))Yes,
vehicleModel is setN0, id_vehicleBrand is not setN0, id_vehicleType is
not set

1 个答案:

答案 0 :(得分:0)

请尝试下面的代码,我更正,你会得到你错误的地方。我希望如此

form role="form-horizontal" action="addVehicleModel.php" method="post">
          车型:           

  <label for="vehicleType">Vehicle Type:</label>
 <div>
       <select class="form-control" onchange="change_vehicleType()" name="vehicleType" id="vehicleTypeID">
<option value="">Select Vehicle Type</option>

        <option value="1">hello</option>

       </select> 

  <label for="vehicleType">Vehicle Brand:</label>
 <div>
       <select class="form-control" onchange="change_vehicleBrand()" name="vehicleBrand" id="vehicleBrandID">
             <option value="">Select Vehicle Brand</option>
                      <option value= "2" >BMW</option>  
       </select> 


      提交       关

<?php 
$mysqli = new mysqli("localhost", "root", "root", "vms");

if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}




 $vehicleModel = $mysqli->real_escape_string($_REQUEST['vehicleModel']);
 $vehicleTypeID = $mysqli->real_escape_string($_REQUEST['vehicleType']);
 $vehicleBrandID = $mysqli->real_escape_string($_REQUEST['vehicleBrand']);


  $sql = "INSERT INTO vehiclemodel (vehicle_Model, id_FKvehicleType,id_FKvehicleBrand) VALUES ('$vehicleModel', '$vehicleTypeID', '$vehicleBrandID')";
 if($mysqli->query($sql) === true){
echo "Records inserted successfully.";
  } else{
 echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
 }



  $mysqli->close();
 ?>