【问题标题】:How to get records from database and show in html format?如何从数据库中获取记录并以 html 格式显示?
【发布时间】:2017-03-20 09:29:56
【问题描述】:

我想从数据库表中检索记录并从章节表中获取 id、标题,并且我想在选择标签中显示章节名称。

当用户选择任何一章时,我想获取章节的 ID 并通过表单发布。

我尝试添加选择查询,但我不知道如何在选择标签中显示它并获取选择章节的 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>
    ?>

【问题讨论】:

标签: php html sql


【解决方案1】:

首先您的html 标记无效您不能在body 之后关闭head 标记 这就是 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->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>

您在此处收到 500 错误的原因:

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

这是因为您将 php 和 html 错误地混合在一起。要么你使用echo来回显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->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>

【讨论】:

  • 我在选择标签中没有得到任何值
  • 你的数据库表上有章节chapterid吗?
  • 是的,我检查了它并添加了与数据库列名称相同的名称,但选择标签仍然是空白的。
猜你喜欢
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
相关资源
最近更新 更多