简体   繁体   English

如何将动态下拉列表中的值提交到另一个数据库?

[英]How do I submit a value from a dynamic dropdown to another database?

I have a HTML form with a dropdown select for choosing a name which is dynamically populated from a MySQL database using PHP. 我有一个带有下拉菜单的HTML表单,用于选择使用PHP从MySQL数据库动态填充的名称。 How would I submit that value or chosen name with the form? 我将如何通过表单提交该值或所选名称?

Here is my dropdown: 这是我的下拉菜单:

<select class="form-control" name="nameselect" required>
        <option value="" disabled selected>Select Name</option>
        <?php while($row1 = mysqli_fetch_array($result1)):?>
        <option value="<?php echo ($row1["fullname"]);?>"><?php echo ($row1["fullname"]);?></option>
        <?php endwhile;?>
</select>
    //first check if it's selected

    if(isset($_POST['save']) && isset($_POST['nameselect'])){

    $name=$_POST['nameselect'];

    //using mysqli prepared statement

    $result=$con->prepare("INSERT INTO tablename VALUES(?, ? )") ; //query
    if ($result){ //if query
    $result->bind_param('ss', $name, $anyothervalue ); //bind the parameters
    $result->execute();   //execute the query
    }

   if($result->affected_rows >0){ //if data is inserted
   echo "Added Successfully!!!"; //success message
   }
   else{ 
   echo "error"; //error message
   }

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM