我目前正在制作一个插入页面,该页面应该允许我向数据库添加数据。但是我最近遇到了一个问题。 我的插入表单包含一个选择,该选择显示链接表中的数据,我希望将 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来判断哪个产品位于哪个位置。
我用来尝试实现它的代码:
经理.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
<?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"/>
<input type="submit" name="cancel" value="Cancel"/>
</form>
</body>
</html>
我希望能够通过表单将这些 id 值从location_id插入到product_location_id中,但是当我尝试执行插入语句时,它会给我错误。
我收到的错误:
注意:未定义的索引:product_location_id在 D:\wamp\www\crudmetlogin2\src\Classes\Manager.php 第 23 行
致命错误:未捕获的异常"PDOException",消息为"SQLSTATE[23000]:完整性约束冲突:1048 第 25 行 D:\wamp\www\crudmetlogin2\src\Classes\Manager.php 中的列"product_location_id"不能为空
PDOException: SQLSTATE[23000]: 完整性约束冲突: 1048 列 'product_location_id' 在第 25 行的 D:\wamp\www\crudmetlogin2\src\Classes\Manager.php 中不能为空
那么我需要做什么才能让它工作呢?
所有错误似乎都归因于这一行代码
$statement->bindValue(4, $_POST["product_location_id"]);
它似乎product_location_id以您的 html 形式存在, 我想product_location_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/>
所以尝试像这样更改选择的名称
<select id="location" name="product_location_id">
<?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">
但是你的PHP脚本称它为product_location_id
$statement->bindValue(4, $_POST["product_location_id"]);
所以将代码更改为
$statement->bindValue(4, $_POST["location"]);
注意
<option>
标记没有name
属性,因此请将其从此行中删除
<option name="location" value="<?=$row["location_id"] ?>"><?=$row["location"] ?></option>
// ^^^^^^^^^^^^^^^ get rid of this