2016-11-19 68 views
0

我在PHP中编写了以下代码以从mysql表中获取详细信息,但我不知道如何以HTML形式显示它们。以HTML格式显示SQL数据

<?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(); 
?> 

上面的代码在echo的帮助下显示网页上的数据,但我希望它在窗体中显示。 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> 

我是新来的PHP连接,这就是为什么有些迷惑。帮助将不胜感激。谢谢

+0

你想粘贴在HTML表单中的PHP值? –

+0

你可以使用PHP来生成整个表单,而你从MySQL获取数据?设置输入的值属性以包含$ row [] ??? – 2016-11-19 13:12:30

+0

设置你的值是这样的'value =“<?php echo $ row ['dsl'];?>”' – Karthi

回答

0

首先你需要有包含你的HTML表单的页面的.php扩展名。

从数据库中获取数据后,你可以将它设置成表单元素像

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

我会使用类似于PHPTAL一个模板ENGIN建议更换。您将您的html页面编写为包含tal:标签的.xhtml文件,标签由模板引擎用来插入您的php值。 Install instructionsexample of use

使用模板引擎的好处在于,您可以从脚本中删除所有html内容,并将显示逻辑保留在xhtml文件中。 php代码只是收集数据并将其分配给模板知道的标签。

您可以将所有行作为数组并将其分配给模板对象中的变量名称。然后在xhtml文件中使用该变量名称来重复表格中的行(或任何其他元素)。

腓:

<?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例如重复每一行,插入元件内容,设置属性和使用条件语句只显示为行索引1.重复方法的输入场的行部分可以用于选择和条件检查中的选项以设置选定属性。 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>