Php 通过表单选择选项插入mysql外键值

Php 通过表单选择选项插入mysql外键值,php,mysql,mysql-workbench,Php,Mysql,Mysql Workbench,我目前正在制作一个插入页面,该页面应允许我向数据库添加数据。然而,我最近遇到了一个问题。 我的插入表单包含一个select,该select显示链接表中的数据,我希望将其id插入到另一个表中 我的数据库如何运行的示例: --------------------------- | location_id | location | |-------------------------| | 1 | Amsterdam | |-------------------------| |

我目前正在制作一个插入页面,该页面应允许我向数据库添加数据。然而,我最近遇到了一个问题。 我的插入表单包含一个select,该select显示链接表中的数据,我希望将其id插入到另一个表中

我的数据库如何运行的示例:

---------------------------
| location_id | location  |
|-------------------------|
|      1      | Amsterdam |
|-------------------------|
|      2      | Hilversum |
|-------------------------|
|      3      | Loosdrecht|
|-------------------------|

-------------------------------------------------------------------------
| product_id | productname | productcode | amount | product_location_id |
|-----------------------------------------------------------------------|
|      1     |   Hammer    |    HK47     |   10   |          1          |
|-----------------------------------------------------------------------|
|      2     |    saw      |    ZW67     |   13   |          3          |
|-----------------------------------------------------------------------|
正如您所看到的,product_location_id是外键,它通过显示第一个表中的主键location_id来判断哪个产品位于哪个位置

我用来尝试实现它的代码:

---------------------------
| location_id | location  |
|-------------------------|
|      1      | Amsterdam |
|-------------------------|
|      2      | Hilversum |
|-------------------------|
|      3      | Loosdrecht|
|-------------------------|

-------------------------------------------------------------------------
| product_id | productname | productcode | amount | product_location_id |
|-----------------------------------------------------------------------|
|      1     |   Hammer    |    HK47     |   10   |          1          |
|-----------------------------------------------------------------------|
|      2     |    saw      |    ZW67     |   13   |          3          |
|-----------------------------------------------------------------------|
Manager.php

    class Manager {

       protected $conn;
    
       function __construct($conn) {
          $this->conn = $conn;
       }
    
      function createProducts() {
         $statement = $this->conn->prepare("INSERT INTO supplies 
         (productname, productcode, amount, product_location_id) VALUES 
         (?,?,?,?)");
        
          $statement->bindValue(1, $_POST["productname"]);
          $statement->bindValue(2, $_POST["productcode"]);
          $statement->bindValue(3, $_POST["amount"]);
          $statement->bindValue(4, $_POST["product_location_id"]);
        
          $statement->execute();
        
       }
    
       function getLocations() {
          $statement = $this->conn->prepare("SELECT location_id, location FROM 
          location");
        
          $statement->execute();
        
          return $statement;
       }
    }
<?php
    require_once 'database.php';
    
    require ("../src/Classes/Manager.php");
    
    $productManager = new Manager($conn);
    
    $getLocatie = $productManager->getLocations();
    
    
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST['create'])) {
            $addProduct = $productManager->createProducts();
            header("Location: read.php");
        }
        
        if(isset($_POST['cancel'])) {
            header("Location: read.php");
        }
    }
?>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta charset="UTF-8">
        <title>Create</title>
    </head>
    <body>
        <form method="POST" class="formHandler">
            <label for="productname">Productname</label><br/>
            <input type="text" id="productname" name="productname" placeholder="productname"/><p/>
            
            <label for="productcode">Productcode</label><br/>
            <input type="text" id="productcode" name="productcode" placeholder="productcode"/><p/>
            
            <label for="aantal">Amount</label><br/>
            <input type="text" id="amount" name="amount" placeholder="amount"/><p/>
            
            <label for="location">Location</label><br/>
            <select id="location" name="location">
                <?php foreach ($getLocation->fetchAll() as $row) { ?>
                <option name="location" value="<?=$row["location_id"] ?>"><?=$row["location"] ?></option>
                <?php } ?>
            </select><p/>
            
            <input type="submit" name="create" value="Create"/>&nbsp;
            <input type="submit" name="cancel" value="Cancel"/>
        </form>
    </body>
</html>
create.php

    class Manager {

       protected $conn;
    
       function __construct($conn) {
          $this->conn = $conn;
       }
    
      function createProducts() {
         $statement = $this->conn->prepare("INSERT INTO supplies 
         (productname, productcode, amount, product_location_id) VALUES 
         (?,?,?,?)");
        
          $statement->bindValue(1, $_POST["productname"]);
          $statement->bindValue(2, $_POST["productcode"]);
          $statement->bindValue(3, $_POST["amount"]);
          $statement->bindValue(4, $_POST["product_location_id"]);
        
          $statement->execute();
        
       }
    
       function getLocations() {
          $statement = $this->conn->prepare("SELECT location_id, location FROM 
          location");
        
          $statement->execute();
        
          return $statement;
       }
    }
<?php
    require_once 'database.php';
    
    require ("../src/Classes/Manager.php");
    
    $productManager = new Manager($conn);
    
    $getLocatie = $productManager->getLocations();
    
    
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST['create'])) {
            $addProduct = $productManager->createProducts();
            header("Location: read.php");
        }
        
        if(isset($_POST['cancel'])) {
            header("Location: read.php");
        }
    }
?>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta charset="UTF-8">
        <title>Create</title>
    </head>
    <body>
        <form method="POST" class="formHandler">
            <label for="productname">Productname</label><br/>
            <input type="text" id="productname" name="productname" placeholder="productname"/><p/>
            
            <label for="productcode">Productcode</label><br/>
            <input type="text" id="productcode" name="productcode" placeholder="productcode"/><p/>
            
            <label for="aantal">Amount</label><br/>
            <input type="text" id="amount" name="amount" placeholder="amount"/><p/>
            
            <label for="location">Location</label><br/>
            <select id="location" name="location">
                <?php foreach ($getLocation->fetchAll() as $row) { ?>
                <option name="location" value="<?=$row["location_id"] ?>"><?=$row["location"] ?></option>
                <?php } ?>
            </select><p/>
            
            <input type="submit" name="create" value="Create"/>&nbsp;
            <input type="submit" name="cancel" value="Cancel"/>
        </form>
    </body>
</html>

创造
产品名称

产品代码

金额

位置


所有的错误似乎都是由于这行代码引起的

  $statement->bindValue(4, $_POST["product_location_id"]);
似乎产品位置id不以html的形式存在, 我想产品位置id应该在这里

<select id="location" name="location">
                <?php foreach ($getLocation->fetchAll() as $row) { ?>
                <option name="location" value="<?=$row["location_id"] ?>"><?=$row["location"] ?></option>
                <?php } ?>
            </select><p/>


您的表单有一个名为
location

<select id="location" name="location">
所以把代码改成

$statement->bindValue(4, $_POST["location"]);
注意
标记没有
名称
属性,因此将其从此行中删除


您是否检查了
var\u dump($\u POST)
?没有
$\u POST[“产品位置\u id”]
-应该是
$\u POST[“位置”]
。。或者重命名输入字段的
名称
。Paul Spiegel当我按下“创建”按钮时,显示数据已填充,没有任何内容为空。尝试使用var_dump(“location”),但它给了我相同的错误,只是location现在未定义。我要重复我自己:您是否检查了
var_dump($\u POST)
以查看其中的内容?Paul Spiegel是的,它包含的数据是我想要的数据。但是,它不想将其插入数据库,但第一条错误消息(注意)讲述了一个不同的故事。
选择
字段的名称是
位置