gpt4 book ai didi

php - 如何将名称转换为 ID

转载 作者:行者123 更新时间:2023-11-30 22:50:30 25 4
gpt4 key购买 nike

我正在尝试将具有外键的 ID 更新到另一个名称表。我有一个下拉菜单,在下拉菜单中我有来自表 NAME_TEST 的名称。我需要选择名称,但我想要的插入内容是:

INSERT INTO (test) values (the value that i need is the ID of selected name)       

代码:

<html> <h1>Update form</h1></html>

<?php
if (isset($_POST['submit'])) {
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = "UPDATE test SET location_name='".$_POST['new_location']."' WHERE id='".$_POST['location']."' LIMIT 1";
$res = mysqli_query($connect, $query);
if (!$res) {
die("Something went wrong");
}
}

// This is the code to load the select options
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM name_test';
$res = mysqli_query($connect, $query);
echo "Choose setup";
$options = array();


while($row = mysqli_fetch_assoc($res)) {
$options[] = $row;
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>"><BR><BR>
<select name="location">
<option value="0">--- Select an option ---</option>
<?php foreach ($options as $option): ?>
<option value="<?= $option['id'] ?>"><?= $option['name'] ?></option>
<?php endforeach; ?>
</select><br /><BR><BR>
<B> New name:</B> <BR> <input type="text" name="new_location"><br /><BR><BR>
<input type="submit" name="submit" value="Update" />
</form>

最佳答案

您可以为此使用join。这个想法是这样的:

INSERT INTO t(nameid)
select nameid
from names n
where n.name = ?;

t 是您要插入的表。 names 是包含名称 id 和名称的表。

关于php - 如何将名称转换为 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28324664/

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