[Solved] How to get records from database and show in html format?


First your html Markup up is invalid you can’t close head tag after body
this is how an html markup looks like :

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

Now this is how your code should look :

<?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")->fetchAll(FETCH_ASSOC);
                    foreach($stmt as $row):?>
                <option value="<?php echo $row['chapterid'];?>"><?php echo $row['chapterName'];?></option>
                <?php
                    endforeach;?>
            </select>
        </form>
    </body>
</html>

The reason your getting the 500 error here :

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

its because you are mixing php and html wrong. its either you echo the html tags using echo

Or :

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

OR

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

4

solved How to get records from database and show in html format?