gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:33:00 24 4
gpt4 key购买 nike

我目前正在制作一个插入页面,应该允许我将数据添加到我的数据库中。但是我最近遇到了一个问题。我的插入表单包含一个选择,它显示链接表中的数据,我希望将其 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 来判断哪个产品位于哪个位置。

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

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

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

我希望能够通过表单将 location_id 中的那些 id 值插入到 product_location_id 中,但是当我尝试执行我的插入语句时它给我错误。

我收到的错误:

Notice: Undefined index: product_location_id in D:\wamp\www\crudmetlogin2\src\Classes\Manager.php on line 23

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_location_id' cannot be null' in D:\wamp\www\crudmetlogin2\src\Classes\Manager.php on line 25

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_location_id' cannot be null in D:\wamp\www\crudmetlogin2\src\Classes\Manager.php on line 25

那么我需要做什么才能让它正常工作?

最佳答案

所有错误似乎都是由于这行代码造成的

  $statement->bindValue(4, $_POST["product_location_id"]);

您的 html 中的 product_location_id 似乎不存在,我想 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的名称

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

关于php - 通过表单选择选项插入 mysql 外键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50306369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com