如何使用php在个人卡中显示MySql表中的数据?

时间:2017-05-09 05:00:48

标签: php mysql mysqli

我一直想在我的网站上这样做,但我找不到合适的方法来做到这一点。任何资源都会很棒。总的来说,我试图在单个引导卡中显示每个数据值。此外,是否可以使卡的数量等于MySql表中的值的数量?我不明白的是如何将PHP集成到HTML中,使页面显示可更改数量的引导卡。我试图用PHP(我在使用它是相当新的)这样做。谢谢您的帮助。所有回复都表示赞赏。

//the following gets values from the two tables, I am trying to get the values from the "data" table 
if (isset($_SESSION['username'])) {
    $userLoggedIn = $_SESSION['username'];
    $user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username='$userLoggedIn'" );
    $user = mysqli_fetch_array($user_details_query);


    $user_id = $user['id'];
    $data_detail_query = mysqli_query($con, "SELECT * FROM data WHERE user_id ='$user_id'");
    $data = mysqli_fetch_array($data_detail_query);

    $num_rows = mysqli_num_rows($data_detail_query);
    }

//The following shows one bootstrap card that I was able to create using one row. 
<div class="card" style="width: 18rem;float:left;margin-top:20px;margin-left:20px;border-color:white;border-width:5px;">

      <div class="card-block">
          <img style="height:250px;width:100%;" src="<?php echo $data['icon']; ?>"  alt="Card image cap">
            <div>
            <h1 class="card-title" ><?php echo $data['name']?><span style="color:#5aff28;float:right" ><?php echo $data['level']; ?></span></h1>
            <h5 class="card-subtitle mb-2 text-muted"><?php echo $data['category']; ?></h5>
            <p class="card-text"><?php echo $data['description']; ?></p>
            <a href="<?php echo $data['link']; ?>" class="card-link">Link</a>
            <a href="<?php echo $data['developer_link']; ?>" class="card-link">Developer Profile</a>

        <form method="post">

              <div style="width:200;float:left;margin-top:5px" class="form-group">
                <input  type="text" class="form-control" id="code" name="code" placeholder="Enter Code">
              </div>

        </form method="post">
         <a href="" name="code_btn" class="btn btn-success">Enter</a>
        </div>

我试图获取名为“data”的表中的每一行,其用户名为$ userLoggedIn,并将每个结果显示在自己的引导卡中。

1 个答案:

答案 0 :(得分:1)

mysqli_fetch_array只是给你NEXT结果,而不是全部。您只需从数据库中获取结果,直到不再有结果,例如:

while (($data = mysqli_fetch_array($data_detail_query))) {
 /// ... do the output as above, e.g.:
 ?><a href="<?php echo $data['link']; ?>" class="card-link">Link</a><?php
}

顺便说一下,确保你没有在这里产生SQL注入漏洞:

$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username='$userLoggedIn'" );
                                                                              ^^^^^^^^^^^^^^^^

使用预备语句或执行正确的字符串引用:

$userLoggedIn = $con->real_escape_string($userLoggedIn);
$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username={$userLoggedIn}" );