简体   繁体   English

使用SELECT选项从PHP插入外键值

[英]Insert Foreign Key value from PHP using SELECT option

I creating an inventory web-base system by using php and php myadmin (InnoDB). 我通过使用php和php myadmin(InnoDB)创建了一个基于清单的Web系统。 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. 当我插入记录时,我想在库存中插入值,我可以在下拉列表中看到(FK)的数据,但是当我向db提交表单数据时,它在字段中作为无输入值返回,并且下拉列表不再存在。 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)) 表库存(id(pk),名称,condition_(fk),产品类型(fk))

Table condition_type( condition_ (pk)) 表condition_type(condition_(pk))

Table producttype( producttype(fk)) 表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 PHP警告:mysqli :: query():无法在第173行的/home/my-path/application/create_new.php中获取mysqli

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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