简体   繁体   中英

Insert Foreign Key value from PHP using SELECT option

I creating an inventory web-base system by using php and php myadmin (InnoDB). I want to insert the value in inventory when I inserting the record, I can see the data of the (FK) in the dropdown but when I submit the form the data to the db, it returns as no input value in the field and the dropdown not there anymore. Is the way I'm using the foreign key in the dropdown wrong?

I have a table that contains multiple foreign keys.

Table Inventory( id (pk), name, condition_(fk), producttype (fk))

Table condition_type( condition_ (pk))

Table producttype( producttype(fk))

<?php
// Include config file
require_once "../config.php";

// Define variables and initialize with empty values
$name = $condition_ = $producttype = "";
$name_err = $condition_err = $producttype_err = "";


$sql2 = "SELECT * FROM condition_type";
$sql4 = "SELECT * FROM producttype";


// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate name
$input_name = trim($_POST["name"]);
if(empty($input_name)){
    $name_err = "Please enter a name.";
}else{
    $name = $input_name;
}

// Validate condition
$input_condition = trim($_POST["condition_"]);
if(empty($input_condition)){
    $condition_err = "Please choose the condition.";     
} else{
    $condition_ = $input_condition;
}

// Validate producttype
$input_producttype = trim($_POST["prodcuttype"]);
if(empty($input_producttype)){
    $producttype_err = "Please enter the product type..";     
} else{
    $producttype = $input_producttype;
}

// Check input errors before inserting in database
if(empty($name_err) && empty($condition_err) && empty($producttype_err)){
    // Prepare an insert statement
    $sql = "INSERT INTO inventory (name, condition_, producttype) VALUES (?, ?, ?)";

    if($stmt = $mysqli->prepare($sql)){
        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("sss", $param_name, $param_condition, $param_producttype);

        // Set parameters
        $param_name = $name;
        $param_condition = $condition;
        $param_producttype = $producttype;

        // Attempt to execute the prepared statement
        if($stmt->execute()){
            // Records created successfully. Redirect to landing page
            header("location: ../application");
            exit();
        } else{
            echo "Something went wrong. Please try again later.";
        }
    }

    // Close statement
    $stmt->close();
}

// Close connection
$mysqli->close();
}
 ?>

-------> This is the form

<div class="form-group <?php echo (!empty($condition_err)) ? 'has-error' 
: ''; ?>">
<label>Condition</label>
</br>
<select id="condition_" name="condition_" class="form-control" value="<? 
php echo $condition_ ;?>">
<option>Please Select Product Condition</option>
<?php
 if($result2 = $mysqli ->query($sql2)){
 while($row = $result2->fetch_array()){
  echo "<option value=".$row['condition_'].">" .$row['condition_']. " 
 </option>";
     }
   }
    ?> 
  </select>
     <span class="help-block"><?php echo $condition_err;?></span>
   </div>
 <div class="form-group <?php echo (!empty($producttype_err)) ? 'has- 
 error' : ''; ?>">
 <label>Product</label>
     </br>
 <select name="producttype" class="form-control" value="<?php echo 
 $producttype ;?>">
  <?php if($result4 = $mysqli ->query($sql4)){
  while($row = $result4->fetch_array()){
  echo "<option value=".$row['producttype'].">" .$row['producttype']. " 
 </option>";
     }
   }
 ?> 
 </select>
<span class="help-block"><?php echo $manufacturer_err;?></span>
  </div>

So when I submit it return as the condition and producttype are empty. I think the error is because of
}

// Close connection
$mysqli->close();
 }

statement that already close. But I don't know to place it.

PHP Warning: mysqli::query(): Couldn't fetch mysqli in /home/my-path/application/create_new.php on line 173

您应该首先通过右键单击“检查元素”来调试html表单,并确保获取“选择”值?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM