gpt4 book ai didi

php - 从数据库检索到下拉列表的数据。用户选择选项然后将该值发送到数据库?

转载 作者:行者123 更新时间:2023-11-29 19:37:20 25 4
gpt4 key购买 nike

现在我一直在尝试找出一种方法,使用 PHP 将用户在 Web 界面上的下拉菜单选择插入到 MySQL 数据库 table_2 中。问题是下拉列表项是从另一个 table_2 的 MySQL 数据库中检索的。有人可以帮帮我吗?先感谢您!下面显示了我正在使用的代码。

 <?php
$con = mysqli_connect("localhost","root","");
$myDB = mysqli_select_db($con, "database");

$sqlSELECT = mysqli_query($con, 'SELECT disastergroup FROM disastergroups');

if (isset($_POST['group']))
{
$group = $_POST['group'];
$test = "SELECT disastergroupid FROM disastergroups WHERE disastergroup = '$group'";
mysqli_query($con, $test);
$test_store = "INSERT INTO events (groupid_FK) VALUES ($test);"
mysqli_query($con,$test_store);
}
else
{
echo "An option must be selected!";
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Title of Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<form action = "detailslog.php" method = "POST">

<label for="groups">Disasters:</label>
<select name = "groups">
<option value = "">Select...</option>
<?php while($row = mysqli_fetch_assoc($sqlSELECT)):;?>
<option><?php $row1['disastergroup'];?></option>
<?php endwhile;?>
</select>
<input type="submit" value="Submit Data">
</form>
</body>
</html>

所以我想要做的是从选项“groups”中获取用户的选择,并使用该值从表disastergroups中获取该值的ID,然后将该ID作为外键存储到表“events”中。这让我苦苦思索。任何帮助将不胜感激!谢谢!

最佳答案

$sql_quer = mysqli_query($con, $test); 
$FK_id = mysqli_fetch_assoc($sql_quer);
$test_store = "INSERT INTO events (groupid_FK) VALUES ($FK_id[disastergroupid]);"
mysqli_query($con,$test_store);

这为查看这篇文章的人解决了我的问题!

关于php - 从数据库检索到下拉列表的数据。用户选择选项然后将该值发送到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518053/

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