N.Yuki
N.Yuki

Reputation: 49

update database through html/php form with drop down menu included

here I have another question, how can we update the database through html/php form with some of the data was populated using dropdown menu?

It is something like this:

User choose to update the asset, and the asset will display the form with the stored data, and they can just directly update the asset using that form again (this is including the dropdown menu).

Hope some of you can get back to me soon. Really appreciate it. Thank you.

Edited:

<form method="POST" name="update" action="update.php">

<h2>EDIT</h2>
<table class="reference" cellspacing="0" cellpadding="0" border="1" width="60%"  id="tablecss">

       <tr>
       <td> Category </td>
       <td> <?php echo '<select name="categoryid">';
       foreach ($category as $data)
       {
       echo '<option'.($row['name']==$data? ' selected' : '').'>'.$data.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Brand </td>
       <td> <?php echo '<select name="brandid">';
       foreach ($brand as $data1)
       {
       echo '<option'.($row['name']==$data1? ' selected' : '').'>'.$data1.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Location </td>
       <td> <?php echo '<select name="locationid">';
       foreach ($location as $data2)
       {
       echo '<option'.($row['name']==$data2? ' selected' : '').'>'.$data2.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Staff </td>
       <td> <?php echo '<select name="staffno">';
       foreach ($staff as $data3)
       {
       echo '<option'.($row['name']==$data3? ' selected' : '').'>'.$data3.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Supplier </td>
       <td> <?php echo '<select name="supplierid">';
       foreach ($supplier as $data4)
       {
       echo '<option'.($row['name']==$data4? ' selected' : '').'>'.$data4.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Project </td>
       <td> <?php echo '<select name="projectid">';
       foreach ($project as $data5)
       {
       echo '<option'.($row['name']==$data5? ' selected' : '').'>'.$data5.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

      <tr>
            <td> <input type="hidden" name="assetid" value="<?php echo "$assetid"; ?>" > </td>
      </tr>

</table>
              <input type="submit" name="submit" value="Update"  onclick="return confirm('Update this?');">
              <input type="button" value="Back" onclick="history.back();">

</form>

So, basically this can't function well. They didn't capture the stored data, but it can update into the database. Any help?

Upvotes: 0

Views: 4017

Answers (1)

Quasdunk
Quasdunk

Reputation: 15230

This is pretty straight forward form processing and you could have found out how to do it with 1 minute of googling...

//formpage.php:

<?php
$data_to_populate_dropdown = array();
//run some sql query or whatever to populate that array
//..or just populate the dropdown statically, then there's no need for this

//if the form has been submitted
$errors = array();
if(isset($_GET['action']) && $_GET['action']=='process') {
  if(isset($_POST['my_dropdown'])) {
    $my_dropdown_value = mysql_real_escape_string($_POST['my_dropdown']);
    //write this to db...
    header('Location: formpage.php'); //redirect to avoid double posting;
  } else {
    $errors[] = 'Some data is missing';
  }
}

if(!empty($errors)) {
  foreach($errors as $error) {
    echo $error;
  }
}
?>


<form action="formpage.php?action=process" method="post">
  <select name="my_dropdown">
    <?php foreach($data_to_populate_dropdown as $key => $value) {
       echo '<option value="'.$key.'">'.$value.'</option>';
    } ?>
  </select>
  <input type="submit" value="OK" />
</form>

This code is not tested and quite abstract, but I think you get the idea.

Upvotes: 1

Related Questions