php - 将 MYSQL 数据从 PHP 传递到 HTML

标签 php html mysql

我一直在尝试使用 PHP 用来自 MYSQL 数据库的数据填充 HTML 表。我遇到的问题是 HTML 文件中的占位符值从不显示 MYSQL 形式的数据。

HTML 文件

<div class="container" style="background-image: url('http://www.MyWebSite.com/OurWorld/EditorBG.png');
-webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover;
background-size: repeat; padding-bottom: 5px;">
<br />
<div class="mainbody" style="background-image: url('http://www.MyWebSite.com/Test/Notepad.png');
    background-repeat: no-repeat; margin-left: auto; margin-right: auto; margin-bottom: 10px;
    width: 646px; height: 800px !important; min-height: 100%; overflow: hidden;">
    <div class="main-form" style="margin-top: 280px; margin-left: 15px;">
        <form method="GET" action="http://www.MyWebSite./Test/SuggestionSchemeForm.php">
            <table align="center" bgcolor="white" BORDER=2 BORDERCOLOR=Black summary="Submitted Suggestions">
                <caption bgcolor="white" BORDER=2 BORDERCOLOR=Black >Submitted Suggestions</caption>
                    <thead>
                        <tr><th>Name</th><th>Site</th><th>Status</th><th>Date</th></tr>
                    </thead>
                    <tbody>
                        <tr><td><input placeholder="name"></td>   <td><input placeholder="site"></td>   <td><input placeholder="status"></td>   <td><input placeholder="date"></td></tr>
                        <tr><td><input placeholder="name"></td>   <td><input placeholder="site"></td>   <td><input placeholder="status"></td>   <td><input placeholder="date"></td></tr>
                        <tr><td><input placeholder="name"></td>   <td><input placeholder="site"></td>   <td><input placeholder="status"></td>   <td><input placeholder="date"></td></tr>
                        <tr><td><input placeholder="name"></td>   <td><input placeholder="site"></td>   <td><input placeholder="status"></td>   <td><input placeholder="date"></td></tr>            
                    </tbody>
            </table>
        </form>
    </div>
</div>

PHP 文件

<?php
Global $USER;
$servername = "";
$username = "";
$password = "";
$dbname = "";
$firstname = $USER->firstname;
$lastname = $USER->lastname;

$testname = "BLEGH";

echo $testname;


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) 
{
   die("Connection failed: " . $conn->connect_error);
}

$sql = mysql_query("SELECT name, site, status, date FROM enquiries")
$result = $conn->query($sql);


while($row = mysql_fetch_array($result MYSQL_ASSOC))
{
   echo "name {$row['name']}".
      "site {$row['site']}".
      "status {$row['status']}".
      "date {$row[date]}";  
}




if ($conn->query($sql) === TRUE) 
{
     echo "New record created successfully";
} 
else 
{
     echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

Header('Location: http://www.MyWebSite.com/index.php');
exit;
?> 

最佳答案

好吧,他们无法显示任何内容。 首先,您在 PHP 代码末尾执行重定向(使用 header “Location”),因此没有任何变量会传递到 HTML,并且 View 将立即刷新。 其次,HTML 不能单独存在,您应该在 HTML 代码中回显从 mysql 检索的变量。

<?php 
    // stuff..
    $resultArray = [];

    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $resultArray[] = $row;
    }
?>

<!-- (...) -->

<?php foreach ($resultArray as $row) { ?>
<tr>
    <td><input type="text" placeholder="name" name="name" value="<?php echo $row['name']?>" /></td>
    <td><input type="text" placeholder="site" name="site" value="<?php echo $row['site']?>" /></td>
    <td><input type="text" placeholder="status" name="status" value="<?php echo $row['status']?>" /></td>
    <td><input type="text" placeholder="date" name="date" value="<?php echo $row['date']?>" /></td>
</tr>
<?php } ?>

关于php - 将 MYSQL 数据从 PHP 传递到 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31026947/

相关文章:

jquery - 如何使用 jQuery Validation Engine 和 Twitter Bootstrap modals 修复定位错误

html - 停止 float div 换行

mysql - 转储大型数据库备份或将大型 sql 文件分割成较小的部分进行转储

mysql - SQL:从另一个数据集添加多列

mysql - 选择所有重复项

PHP 创建文章列表的方法

PHP 函数在 MySQL 中的替代

html - 在 Bootstrap 框架中禁用触摸设备的缩放

php - mysql在一个字段中存储多个条目

php - 将查询结果保存到 CSV 并在服务器上创建文件