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

时间:2018-08-20 15:58:13

标签: php html

我有一个带有下拉菜单的HTML表单,用于选择使用PHP从MySQL数据库动态填充的名称。我将如何通过表单提交该值或所选名称?

这是我的下拉菜单:

<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>

1 个答案:

答案 0 :(得分:0)

    //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
   }

希望这会有所帮助