【问题标题】:Data retrieved from database to Dropdown list. User selects option and then send that value to Database?从数据库检索到下拉列表的数据。用户选择选项,然后将该值发送到数据库?
【发布时间】:2017-01-07 05:13:17
【问题描述】:

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

所以我想要做的是从选项“组”中获取用户的选择,并使用该值从表灾难组中获取该值的 ID,然后将该 ID 作为外键存储到表“事件”中.这一直让我想不通。任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 您在$_POST['group'] 中获得价值了吗?
  • @Akshay 是的,我是。我通过 echo $_POST['group'] 检查了网页,我看到了选定的选项。
  • 那你遇到了什么问题?

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


【解决方案1】:
$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); 

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

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多