Php 如何从数据库中获取记录并以html格式显示?

Php 如何从数据库中获取记录并以html格式显示?,php,html,sql,Php,Html,Sql,我想从数据库表中检索记录,从章节表中获取id和标题,并在select标记中显示章节名称 当用户选择任何一个章节时,我想获得章节的id并通过表单发布 我试图添加select查询,但我不知道如何在select标记中显示该查询并获取有关选择的章节的id <?php ini_set('display_errors', 1); error_reporting(1); ini_set('error_reporting', E_ALL); $dbh = new PDO('mysql

我想从数据库表中检索记录,从章节表中获取id和标题,并在select标记中显示章节名称

当用户选择任何一个章节时,我想获得章节的id并通过表单发布

我试图添加select查询,但我不知道如何在select标记中显示该查询并获取有关选择的章节的id

     <?php
    ini_set('display_errors', 1);
error_reporting(1); 
ini_set('error_reporting', E_ALL);

$dbh = new PDO('mysql:host;dbname','', '');

    $stmt = $dbh->prepare("SELECT * FROM chapters");
    $stmt->execute();
    $stmt->fetchAll(PDO::FETCH_ASSOC);
?>


<!DOCTYPE html>
<html>
    <head>
        <body>


    <form action="fileUpload.php" method="post" enctype="multipart/form-data">
    <p> Select image to upload:</p>
    <input name = "file" type="file" id="fileToUpload"><br><br>

    <input type="submit" value = "Upload Image">

    </form>

        </body>

    </head>

</html>

选择要上载的图像:



有人能帮忙吗?谢谢

编辑:

    <!DOCTYPE html>
<html>
    <head>
        <body>
    <?php
         <select>

          foreach ($stmt as $row)
        {
            <option value=$row['chapterName']>$row['chapterName']</option>
        }

            </select>
    ?>

首先,您的
html
标记无效,您不能在
正文之后关闭
头部
标记 这是html标记的外观:

<!DOCTYPE html>
<html>
  <head>
    <title>This is a title </title>
    META TAGs
  </head>
<body
 BODY CONTENT
</body>
</html>

<?php
    ini_set('display_errors', 1);
    error_reporting(1);
    ini_set('error_reporting', E_ALL);

    $dbh = new PDO('mysql:host=host;dbname=db', 'airman', 'pass');
    ?>
<!DOCTYPE html>
<html>
    <head> </head>
    <body>
        <form action="fileUpload.php" method="post" enctype="multipart/form-data">
            <p> Select image to upload:</p>
            <input name = "file" type="file" id="fileToUpload"><br><br>
            <input type="submit" value = "Upload Image">
            <select name="chapters">
                <?php
                    $stmt = $dbh->prepare("SELECT * FROM chapters");
                    $stmt->execute();
                    $results = $stmt->fetchall(PDO::FETCH_ASSOC);

                    if(count($results > 0)){
                        foreach($results as $row):?>
                         <option value="<?php echo $row['chapterid'];?>"><?php echo $row['chapterName'];?></option>
                    <?php
                        endforeach;
                    }else{?>

                        <option value="0">No data found</option>
                    <?php}?>


            </select>
        </form>
    </body>
</html>

选择要上载的图像:



没有找到任何数据
这些将帮助您启动我在select标记中没有得到任何值您的数据库表上有章节
章节ID
吗?是的,我检查了它并添加了与数据库列名相同的名称,但select标记仍然为空。让我们来看看。
  foreach ($stmt as $row)
        {
            <option value=$row['chapterName']>$row['chapterName']</option>
        }
<?php
    ini_set('display_errors', 1);
    error_reporting(1);
    ini_set('error_reporting', E_ALL);

    $dbh = new PDO('mysql:host=host;dbname=db', 'airman', 'pass');
    ?>
<!DOCTYPE html>
<html>
    <head> </head>
    <body>
        <form action="fileUpload.php" method="post" enctype="multipart/form-data">
            <p> Select image to upload:</p>
            <input name = "file" type="file" id="fileToUpload"><br><br>
            <input type="submit" value = "Upload Image">
            <select name="chapters">
                <?php
                    $stmt = $dbh->query("SELECT * FROM chapters");
                    while($row = $stmt->fetchall(FETCH_ASSOC)):?>
                <option value="<?php echo $row['chapterid'];?>"><?php echo $row['chapterName'];?></option>
                <?php
                    endwhile;?>
            </select>
        </form>
    </body>
</html>
<?php
    ini_set('display_errors', 1);
    error_reporting(1);
    ini_set('error_reporting', E_ALL);

    $dbh = new PDO('mysql:host=host;dbname=db', 'airman', 'pass');
    ?>
<!DOCTYPE html>
<html>
    <head> </head>
    <body>
        <form action="fileUpload.php" method="post" enctype="multipart/form-data">
            <p> Select image to upload:</p>
            <input name = "file" type="file" id="fileToUpload"><br><br>
            <input type="submit" value = "Upload Image">
            <select name="chapters">
                <?php
                    $stmt = $dbh->prepare("SELECT * FROM chapters");
                    $stmt->execute();
                    $results = $stmt->fetchall(PDO::FETCH_ASSOC);

                    if(count($results > 0)){
                        foreach($results as $row):?>
                         <option value="<?php echo $row['chapterid'];?>"><?php echo $row['chapterName'];?></option>
                    <?php
                        endforeach;
                    }else{?>

                        <option value="0">No data found</option>
                    <?php}?>


            </select>
        </form>
    </body>
</html>