简体   繁体   English

以HTML格式显示SQL数据

[英]Displaying SQL data in HTML form

I wrote following code in PHP to get the details from the mysql table but I dont know how to display them in HTML form. 我用PHP编写了以下代码,以从mysql表中获取详细信息,但我不知道如何以HTML形式显示它们。

<?php
$servername = "localhost";
$username = "root";
$password = "icsk";
$dbname = "yusuf";

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

$sql = "SELECT dsl, Fname, Lname, Cid, pack FROM homereg";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo  $row['dsl']. " " . $row['Fname']. " " . $row['Lname']. " " . $row['cid']. " " . $row['pack'] ."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

The above code displays the data on the webpage with the help of echo but i want it to be displayed in form. 上面的代码借助echo将数据显示在网页上,但我希望它以表格形式显示。 HTML form has following code: HTML表单具有以下代码:

<form method="POST" name = "frm" action="homerenew.php">

    <div align="center">
        <table border="1" width="425">
            <tr>
                <td width="223"><font face="Georgia"><b>Subscription Number</b></font></td>
                <td width="186"><input type="text" name="T1" size="20"></td>
            </tr>
            <tr>
                <td width="223"><font face="Georgia"><b>Your Current Pack</b></font></td>
                <td width="186"><input type="text" name="T2" size="20"></td>
            </tr>
            <tr>
                <td width="223"><font face="Georgia"><b>Renewal Options</b></font></td>
                <td width="186"><select size="1" name="D1">
                <option value = "1 Month">1 Month</option>
                <option value = "2 Months">6 Months</option>
                <option value = "1 Year>1 Year</option>
                </select></td>
            </tr>
            <tr>
                <td width="223"><font face="Georgia"><b>Balance Payable</b></font></td>
                <td width="186"><input type="text" name="T3" size="20"></td>
            </tr>
        </table>
    </div>
    <p align="center"><input type="submit" value="Renew" name="B1">&nbsp;&nbsp;&nbsp;
    <input type="reset" value="Reset" name="B2"></p>
</form>

I am new to PHP connectivity that is why little confused. 我是PHP连接的新手,这就是为什么有点困惑的原因。 Help will be greatly appreciated. 帮助将不胜感激。 Thank you 谢谢

First of all you need to have .php extension of the page containing your HTML form. 首先,您需要包含HTML表单的页面的.php扩展名。

After fetching data from database you could set it into form element like 从数据库中获取数据后,您可以将其设置为表单元素,例如

<input type="text" name="T1" size="20" value="<?php echo $row['dsl'];?>">

I would recomend using a templating engin similar to PHPTal. 我建议使用类似于PHPTal的模板引擎。 You write your html page as an .xhtml file that contains tal: tags that are used by the template engine to insert your php values. 您将html页面编写为包含tal:标记的.xhtml文件,模板引擎使用该标记插入php值。 Install instructions , example of use . 安装说明使用示例

The benfit of using a template engine is you can remove all html content from your scripts and leave the display logic in the xhtml file. 使用模板引擎的好处是您可以从脚本中删除所有html内容,并将显示逻辑保留在xhtml文件中。 The php code just gathers the data and assigns it to labels your template knows about. php代码只是收集数据并将其分配给模板知道的标签。

You can get all rows as an array and assign it to a variable name in the template object in php. 您可以将所有行作为数组获取,并将其分配给php中的模板对象中的变量名称。 That variable name is then used in the xhtml file to repeat rows in your table (or any other element). 然后在xhtml文件中使用该变量名来重复表(或任何其他元素)中的行。

Php: Php:

<?php
require_once 'PHPTAL.php';
$rows = getMyDBRows();
// create a new template object
$template = new PHPTAL('my_template_file.xhtml');
$template->rows = $rows;

// execute the template
try {
    echo $template->execute();
}
catch (Exception $e){
    echo $e;
}

Xhtml rows section with example of repeating each row, inserting element contents, setting attributes and using a conditional statement to only display the input field for the row with index 1. Repeat method can be used for options in selects and conditional check for setting selected attribute. Xhtml行部分,其中包含重复每一行,插入元素内容,设置属性以及使用条件语句仅显示索引为1的行的输入字段的示例。Repeat方法可用于selects中的选项和设置所选属性的条件检查。 Phptal requires strict xhtml structure any errors in your xhtml and the page will return an error identifying where there was a problem. Phptal要求严格的xhtml结构,如果您的xhtml中有任何错误,则该页面将返回错误,指出出现问题的位置。

<tr tal:repeat="row rows">
    <td tal:content="row/dsl"><\td>
    <td tal:content="row/fname"><\td>
    <td><input name="lname" tal:attributes="value row/lname, style php: repeat.row.id EQ 1?'display:inline-block':'display:none" tal:content="row/lname"></input>
    ......
<\tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM