如何通过
<select\>
输出和编辑多对多表的列。
我有三个表:`filial_work',`department_work`,`contact_work'。
在其中,我设法仅输出已分配的 `id_filial(id_department)` ,但我需要输出所有内容并且可以更改它们。
________________________
**孝顺工作**:
```
id_孝顺 |孝顺
```
________________________
**部门工作**:
```
id_部门 |部门 |部门_电子邮件
```
________________________
**联系工作:**
```
id_联系方式 | id_孝顺 | id_部门 |全名 |电子邮件用户 |工作编号 |电话号码
```
________________________
用于从 contact_work 表中按 ip 输出字符串的脚本
<?php
include "../src/php/connect_db_tel-sprav-contact.php";
$id_contact = $_GET['id'];
$result = mysqli_query($induction, "SELECT * FROM `contact_work` WHERE `id_contact` = '$id_contact'");
$result = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/src/css/Create_contact_style.css">
<title>Editing a contact</title></head>
<body>
<header><img src="/src/img/01-logo.webp" alt="Centrospas-Yugoria logo">
<h1 class="logo">Editing a contact</h1></header>
<nav></nav>
<main></main>
<div class="visual_create_new_user">
<form action="/src/php/script_change_contact.php " method="post"> //script for outputting a string by ip from the
contact_work table<input type="hidden" name="id_contact" value="<?= $result['id_contact'] ?>">
<p>
<button onclick="location.href='/src/TEST_Admin_Contact.php'">Back to contacts</button>
</p>
<p>Change Branch: <select>
<option><?= $result['id_filial'] ?></option>
</select></p>
<p>Change Department: <select>
<option><?= $result['id_department'] ?></option>
</select></p>
<p>Change the Position: <input type="text" name="_post" value="<?= $result['post'] ?>"></p>
<p>Change your FULL NAME: <input required type="text" name="full_name" value="<?= $result['full_name'] ?>"></p>
<p>Change Email: <input type="text" name="email_user" value="<?= $result['email_user'] ?>"></p>
<p>Change the work phone number: <input type="text" name="work_number" value="<?= $result['work_number'] ?>"></p>
<p>Change personal phone number: <input type="text" name="tel_number" value="<?= $result['tel_number'] ?>"></p>
<p>
<button type="submit">Change</button>
</p>
</form>
</div>
</body>
</html>
为了使用链接表中的值填充下拉框,您需要从此表中选择它们。
因此你的代码可能是这样的
// select your record
$id_contact = $_GET['id'] ?? 0;
$sql = "SELECT * FROM `contact_work` WHERE `id_contact` = ?";
$contact = $db->execute_query($sql, [$id_contact])->fetch_assoc();
// select values from a linked table
$sql = "SELECT id, department FROM `department_work`";
$departments = $db->query($sql)->fetch_all(MYSQLI_ASSOC);
然后你可以像这样创建你的下拉菜单。
<p>Change Department:
<select name = "id_department">
<?php foreach ($departments as $dep): ?>
<?php $selected = ($dep['id'] === $contact['id_department']) ? "selected" : "" ?>
<option value="<?= htmlspecialchars($dep['id']) ?" <?= $selected ?>>
<?= htmlspecialchars($dep['department']) ?>
</option>
<?php endforeach ?>
</select>
</p>
记下我所做的更改