Yusuf M Yusuf M - 9 months ago 38
PHP Question

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.

$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";

The above code displays the data on the webpage with the help of echo but i want it to be displayed in form.
HTML form has following code:

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

<div align="center">
<table border="1" width="425">
<td width="223"><font face="Georgia"><b>Subscription Number</b></font></td>
<td width="186"><input type="text" name="T1" size="20"></td>
<td width="223"><font face="Georgia"><b>Your Current Pack</b></font></td>
<td width="186"><input type="text" name="T2" size="20"></td>
<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>
<td width="223"><font face="Georgia"><b>Balance Payable</b></font></td>
<td width="186"><input type="text" name="T3" size="20"></td>
<p align="center"><input type="submit" value="Renew" name="B1">&nbsp;&nbsp;&nbsp;
<input type="reset" value="Reset" name="B2"></p>

I am new to PHP connectivity that is why little confused. Help will be greatly appreciated. Thank you

Answer Source

First of all you need to have .php extension of the page containing your HTML form.

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'];?>">