Search code examples
phphtmlsqlconnectivity

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
$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. HTML form has following code:

<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. Help will be greatly appreciated. Thank you


Solution

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