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

时间:2017-01-07 05:13:17

标签: php mysql database select drop-down-menu

现在我一直试图找到一种方法,使用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>

所以我想做的就是从选项&#34;群组&#34;中选择用户。并使用该值从表灾难组中获取该值的ID,然后将该ID存储到表中&#34; events&#34;作为外键。这让我很难理解。任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

$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); 

这解决了查看此帖子的任何人的问题!