使用PHP在表中显示MySQL数据库中的数据并使其可编辑

时间:2016-12-01 21:31:11

标签: php mysql

我需要帮助让我的数据库表显示在我的php页面上。有没有人有他们可以帮助我的建议?我知道这可能是前一个问题的重复,但非常感谢任何帮助。我得到了要显示的数据,但现在我不能以表格形式提出任何建议吗?

<html lang="en">
<head>
<title>Music Database</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
    <meta charset="utf-8" />
    <title>Music List Storage</title>
</head>
<body>
<center>
    <header><h1><center>Music Playlist for Month 1</center></header>

    <?php

    $con = mysqli_connect('localhost', 'root', 'root', 'music_database');

    if(!$con)
    {
        die("Can not connect: " . mysqli_error($con));
    }

    if(!mysqli_select_db($con, 'music_database'))
    {
        echo 'Database Not Selected!';
    }


    $sql = "SELECT * FROM month1";
    $query = mysqli_query($con, $sql);

    if (!$query) 
    { // add this check.
        die('Invalid query: ' . mysqli_error($con));
    } else{




    echo "<table border=1>
    <tr>
    <th>Song Title</th>
    <th>Song Artist</th>
    <th>Song Album</th>
    <th>Year Released</th>
    <th>Month Played</th>
    <th>Day of the Week Played</th>
    <th>Date Played</th>
    <th>Time Played</th>
    </tr>";




      while($record = mysqli_fetch_assoc($query))
      {
        echo "<tr>";
            var_dump($record);
        echo "</tr>";

      }

    } 
     mysqli_close($con);
    ?>

    </table>

    <meta charset="utf-8" />
    <title>Music List Storage</title>
</head>
<body>
<center>
    <header><h1><center>Music Playlist for Month 1</center></header>

    <?php

    $con = mysqli_connect('localhost', 'root', 'root', 'music_database');

    if(!$con)
    {
        die("Can not connect: " . mysqli_error($con));
    }

    if(!mysqli_select_db($con, 'music_database'))
    {
        echo 'Database Not Selected!';
    }


    $sql = "SELECT * FROM month1";
    $query = mysqli_query($con, $sql);

    if (!$query) 
    { // add this check.
        die('Invalid query: ' . mysqli_error($con));
    } else{




    echo "<table border=1>
    <tr>
    <th>Song Title</th>
    <th>Song Artist</th>
    <th>Song Album</th>
    <th>Year Released</th>
    <th>Month Played</th>
    <th>Day of the Week Played</th>
    <th>Date Played</th>
    <th>Time Played</th>
    </tr>";




      while($record = mysqli_fetch_assoc($query))
      {
        echo "<tr>";
            var_dump($record);
        echo "</tr>";

      }

    } 
     mysqli_close($con);
    ?>

    </table>

    </body>

    </html>

2 个答案:

答案 0 :(得分:0)

你必须将$ con变量传递给mysqli_error() 像这样,它需要连接变量

if(!$con)
{
    die("Can not connect: " . mysql_error($con));
}

另外,如果你想让它们可以编辑,那么简单地改变你的while循环:

    echo "<tr>";
    echo "<td>" . "<input type='text' value='" . $record['songtitle'] . "'/>". "</td>";
    .
    .
    echo "</tr>";

答案 1 :(得分:0)

使用此代码formet

while($record = mysqli_fetch_array($query))
      {
        echo "<tr>
              <td>'.$record['title'].'</td>
              <td>'.$record['Album'].'</td>
              <td>'.$record['Year '].'</td>
              <td>.'$record['Month'].'</td>
              <td>'.$record['Day'].'</td>
              <td>'.$record['Date'].'</td>
              <td>'.$record['Time '].'</td>
        </tr>";
  }