doumei9589 2016-04-30 07:45
浏览 20
已采纳

带有选定数据的循环表单

I currently need to create a table that auto fills depending on how many entities there is in the database.

I currently have a simple select statement;

   $articleID = $_GET['id'];
   $stmt = $pdo->query('SELECT * FROM article WHERE articleID = "' .    $articleID . '"');
   $result = $stmt->fetch(PDO::FETCH_ASSOC);
   $stmt2 = $pdo->query('SELECT * FROM comments WHERE artID = "' . $articleID . '"');
   $result2 = $stmt2->fetch(PDO::FETCH_ASSOC);

I tried using a Form but that only stores one piece of data.

  • 写回答

1条回答 默认 最新

  • douzengjian1535 2016-04-30 12:10
    关注

    From your SQL code I gather that articleId is the primary key the article table and that artId in the comments table is a foreign key.

    So your first query should return only on result hence the fetch method. The second query on the other hand could returns many rows, but the fetch method only returns the first one. To get an array of results you should use the fetchAll method and loop over the resulting array to show all the comments.

    Here is a code with the two resultset dumped to help you see the structure. I also gave you a simple example of how you could loop over the second result.

    $articleID = $_GET['id'];
    $stmt = $pdo->query('SELECT * FROM article WHERE articleID = "' .    $articleID . '"');
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
    $stmt2 = $pdo->query('SELECT * FROM comments WHERE artID = "' . $articleID . '"');
     $result2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
    
    var_dump($result);
    var_dump($result2);
    
    foreach($result2 as $comment){
       echo '<p>'.$comment['text'].'<p>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)