douhezhang8932 2013-08-04 17:07
浏览 52
已采纳

PHP / SQL - 使用Select元素显示/链接到数据库中的行

I'm building a CRUD app with PHP, and I need some help trying to simplify the user experience. Basically, the user creates a new "Project" with a pre-assigned ID number. This ID number acts as the primary key. Upon submission of the form, a row is created in a "Projects" table in my database.

After the project is created, I want to use another form to add storage transactions to the project (for contextual purposes, I'm tracking warehouse storage). Initially, was going to have the user re-add the ID number (the primary key entered when the project was created), but there are lots of opportunities for human error if the ID number is entered incorrectly.

Current Task: I want to create a "Select" element on my form that lists all ID numbers that have been entered. Then, I can just select one of the projects from that drop down and have the transaction automatically applied. How can I do this?

Thank you in advance for your help!

  • 写回答

2条回答 默认 最新

  • douxingsuo8809 2013-08-04 23:15
    关注

    First, you'll have to do a query (using PHP) to pull a list of all the project records in the project table from that database. When you do the query for that, you'll have a result set as an array of each project in the projects table that you can iterate through using a loop to populate a drop-down box.

    First get the projects (assuming a column name of id that you're using for the drop-down selection):

    <php $projects = mysqli_query($db, "SELECT id FROM PROJECTS_TABLE"); ?>
    

    Then, to display the drop down selection box:

    <form name="formWhatever">
    ...
    <select name="project_id">
    <?php foreach ($projects as $project) { 
          $result = mysqli_fetch_array($projects, MYSQLI_ASSOC)?>
      <option value="<?php echo $result[id]; ?>">Project #<?php echo $result[id]; ?></option>
    <?php } ?>
    </select>
    ...
    </form
    

    I think I also like @pajaja's idea for using an index page to eliminate a long drop-down box as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥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代码代写,需写出详细代码,代价私