doushang4293 2015-02-07 12:55
浏览 26
已采纳

单独为数据库字段使用连接变量

I am new to stackoverflow and am after a little help. I have already tried searching for what I am about to ask, but cannot find any relevant topics, so here goes:

I have a PHP page, that gets a list of venues and it's town/city from a table within a MySQL database, which I have then concatenated to populate a dropdown, e.g. each <option> will display "[venue], [town/city]".

What I am trying to do is when the user selects one of the options, I want to store the [venue] and [town/city] as separate fields in another table within the MySQL database.

I would really appreciate any help.

  • 写回答

2条回答 默认 最新

  • dongxun3777 2015-02-07 13:21
    关注

    I think the best way is to put the datarecord ids (primary key value) into the value attribute of the <option> tags, then on the server side requery the data of the selected id from the database. This way your form can't be manipulated with unwanted data.

    To be more clear, I would do it like in this simplified semi pseudocode:

    <?php
    $action = isset($_GET['action']) ? $_GET['action'] : null;
    
    if($action == "save") {
      $id = $_POST['venue'];
    
      if(!empty($id)) {
        /* fetch data belonging to $id from database and then save venue and town/city
           as separate fields in another table */
      }
    }
    
    /* fetch all data for the selectbox from the db and store it in $data */
    ?>
    <form action="?action=save" method="post">
      <select name="venue" size="1">
        /* iterate through $data and create $id and $caption */
        <option value="<?php echo $id; ?>"><?php echo $caption; ?></option>
        /* iteration end */
      </select>
    
      <input type="submit" value="save" />
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)