Hi
I am new in PHP. I have used dropdown to bind with my sql data. It is ok
But I could not insert data into table only from dropdown. Other controls like text can be inserted.
I have given code below.
Pls advise me.
Maideen

<?php require_once '../inc/header.php'; ?>
      <div class="portlet light bordered">
          <div class="portlet-title">
              <div class="caption">
                  <i class="icon-social-dribbble font-green"></i>
                  <span class="caption-subject font-green bold uppercase">Parameter</span>
              </div>
          </div>
         <form class="forget-form" action="../classes/cls.parameter.php" method="POST">
             <div class="portlet-body">
                 <div class="form-group">
                     <label for="default" class="control-label">Parameter Details</label>
                     <input id="default" type="text" class="form-control" placeholder="Parameter Details" name="paramdetails"> 
                 </div>
                 <div class="form-group">
                     <label for="single" class="control-label">Parameter head</label>
                     <select id="paramhead" class="form-control select2" name="paramhead">
                         <option>-- Select --</option>
                         <?php
                               $sql  = "select * from tbl_paramhead order by paramhead";
                               $stmt   = $pdo->prepare($sql);
                               $stmt->execute();
                                   while ($row = $stmt->fetch())
                                   {
                                     echo '<option value>' .$row['paramhead']. '</option>'; 
                                   } 
                         ?>
                     </select>
                 </div>
             </div>
         <div class="form-actions">
             <button type="submit" class="btn green uppercase btn btn-danger mt-ladda-btn ladda-button" data-style="zoom-out" name="paramhead">Submit</button>
         </div> 
     </form>    
     </div>
 <?php require_once '../inc/footer.php'; ?>

<?php
  require_once '../inc/config.php';
  if(isset($_POST['paramhead']))
  {
      if($_SERVER["REQUEST_METHOD"] == "POST")
      {
        $paramhead =$_POST['paramhead'];
        $paramdetails =$_POST['paramdetails'];
       $bool = true;
       $sql="insert into tbl_parameter(paramhead,paramdetails) values ('$paramhead','$paramdetails')";
       $stmt=$pdo->prepare($sql);
       $stmt->execute();
       $pdo = null;
       print '<script>alert("Saved");</script>';
       print '<script>window.location.assign("../admin/parameter.php");</script>';      
     }
 }  
 ?>

Dani AI

Generated

A few focused notes that complete what started and make the form robust.

The immediate cause was that the option elements were being rendered without a usable value attribute, so the browser did not send the expected value. A second, easy-to-miss problem is the submit button being named paramhead — having a form control and the submit button share the same name can overwrite or confuse the posted value. Fix both: give each <option> a proper value (preferably the row id) and remove or rename the button name attribute.

Quick checks and debugging

  • Inspect the page source or use devtools to confirm each <option> has value="...".
  • Submit and log the raw POST payload (var_dump($_POST) or error_log(json_encode($_POST))) to see exactly what was sent.
  • Make the first choice a disabled placeholder with an empty value, or add the required attribute so an empty selection is impossible.

Safer example patterns (apply these instead of interpolating variables directly)

  • Generate options using the DB id and escape output:
    foreach ($pdo->query('SELECT id,paramhead FROM tbl_paramhead ORDER BY paramhead') as $r) {
      printf(
          '<option value="%d">%s</option>' . "\n",
          (int)$r['id'],
          htmlspecialchars($r['paramhead'], ENT_QUOTES)
      );
    }
  • Insert with PDO placeholders and error mode on:
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sth = $pdo->prepare('INSERT INTO tbl_parameter (paramhead, paramdetails) VALUES (:paramhead, :paramdetails)');
    $sth->execute([':paramhead' => $paramhead, ':paramdetails' => $paramdetails]);

Final checklist

  • Ensure option values exist and are escaped.
  • Do not give the submit button the same name as a form field.
  • Validate server‑side that paramhead is nonempty before inserting.
  • Use prepared statements (placeholders) and enable PDO exceptions for meaningful errors.

Fixing the option values plus removing the submit-name collision will resolve the original problem; the PDO patterns above add safety and easier debugging.

You're not giving the option a value so no data is getting passed back in the POST for that variable.

You need to chnage this:
echo '<option value>' .$row['paramhead']. '</option>';

to:
echo '<option value="' . $row['paramhead'] . '">' .$row['paramhead']. '</option>';

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.