【问题标题】:Insert Foreign Key value from PHP using SELECT option使用 SELECT 选项从 PHP 插入外键值
【发布时间】:2019-04-11 07:39:07
【问题描述】:

我使用 php 和 php myadmin (InnoDB) 创建了一个基于 Web 的库存系统。我想在插入记录时在库存中插入值,我可以在下拉列表中看到(FK)的数据,但是当我将表单数据提交到数据库时,它返回字段中没有输入值,并且下拉菜单不再存在。我在下拉菜单中使用外键的方式是否错误?

我有一个包含多个外键的表。

表库存( 身份证(PK), 姓名, 条件_(fk), 产品类型(fk))

表格条件_type( 条件_(pk))

表产品类型( 产品类型(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();
}
 ?>

------->这是表格

<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>

所以当我提交它时返回,因为条件和产品类型为空。我认为错误是由于
}

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

已经关闭的声明。但是不知道怎么放。

PHP 警告:mysqli::query():无法在第 173 行的 /home/my-path/application/create_new.php 中获取 mysqli

【问题讨论】:

  • 我尝试删除数据库的关闭连接,它可以工作.. 但下一个问题是我应该将关闭连接放在哪里,并且插入也不会进入 T.T

标签: php mysql


【解决方案1】:

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

【讨论】:

  • 已经搞定了,在提交表单之前select值是有的,但是当我提交的时候select值没有了。
  • 请通过编写像这样的测试查询$stmt-&gt;bind_param("sss", 'param_name', 'param_condition', 'param_producttype'); 来确保查询是否正常工作
  • 外键调用的查询?
  • 我写了一些字符串而不是变量来检查查询是对还是错?请写下查询并告诉我。
  • $stmt->bind_param("sss", 'param_name', 'param_condition', 'param_producttype');这个查询已经存在了。
猜你喜欢
  • 2018-10-22
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多