通过html / php表单更新数据库,包括下拉菜单

时间:2011-12-22 10:43:15

标签: php html drop-down-menu

这里我有另一个问题,我们如何通过html / php表单更新数据库,其中一些数据是使用下拉菜单填充的?

这是这样的:

用户选择更新资产,资产将显示包含存储数据的表单,他们可以再次使用该表单直接更新资产(包括下拉菜单)。

希望你们中的一些人能尽快回复我。真的很感激。 谢谢。

编辑:

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

所以,基本上这不能很好地运作。它们不捕获存储的数据,但可以更新到数据库中。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

这是非常直接的表格处理,你可以通过1分钟的谷歌搜索找到如何做到这一点......

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

这段代码没有经过测试,也很抽象,但我认为你明白了。