0

This is my html form where i fetch a dropdown value from a table. i want to insert this dropdown value to a another table.

from_rate:<input type="text" name="from_rate" placeholder="$">
   to_rate:<input type="text" name="to_rate" placeholder="$">
   <label for="receive">From Gateway:</label>
   <?php
   $option="";
   $query="SELECT * FROM settings WHERE TRIM(usd_gateway)> '' ";
   $result=mysqli_query($con,$query);  
   ?>      
<select name='from_gateway' class='form-control col-8'>
   <?php
    while($row = mysqli_fetch_assoc($result)) {
        $currency=$row['usd_gateway'];
        echo "<option value='$currency'>$currency</option>";
    }
   ?>
  <input type="submit" value="Submit" name="submit">

my php code is following

if (isset($_POST['submit'])){
    if (empty($_POST['from_rate']) or empty ($_POST['to_rate'])){
        $err= "All field required";
    }
    else {
        $from_rate=$_POST['from_rate'];
        $to_rate=$_POST['to_rate'];
        $from_gateway=$_POST['from_gateway'];
        $to_gateway=$_POST['to_gateway'];
        $status='1';
    }
    if (empty($err)){
        $sql="INSERT INTO gateway (from_rate, to_rate, from_gateway, status) VALUES ('$from_rate','$to_rate','$from_gateway','$to_gateway','$status')";
        $result=mysqli_query($con,$sql);
        echo "Gateway direction setup completed";
    }
    
}

everything works perfectly when i removed the dropdown from the form.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Hi and welcome to stackoverflow. You are wide open to SQL injection attacks, learn how to use [prepared statements](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) instead of passing your post values directly to your query. Also you are not checking for mysqli errors, use proper error reporting to have feedback on why your query is not executing, [How to get the error message in MySQLi?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param/22662582#22662582) – Dimitris Filippou May 22 '21 at 11:37

2 Answers2

0

I have just solved the problem by using foreach function. this may be helpful for someone who visiting this page.

-1

I noticed that you opened select tag but you didn't close it. try to close the select tag

<select name='from_gateway' class='form-control col-8'>
   <?php
    while($row = mysqli_fetch_assoc($result)) {
        $currency=$row['usd_gateway'];
        echo "<option value='$currency'>$currency</option>";
    }
   ?>
</select>
  <input type="submit" value="Submit" name="submit">