jonel timblaco jonel timblaco - 14 days ago 5
PHP Question

how to get the value from database to textbox in php

if i click the edit button on the table i want to display the value of ID from database to text box

TABLE.PHP

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>ID</th>
<th>FullName</th>
<th>Purpose(s)</th>
<th>Requestedby</th>
<th>DateRequested</th>
<th>DateInspected</th>
<th>DateSubmitted</th>
<th> Action </th>
<th> Action </th>
</tr>
</thead>
<tbody>

<?php
$conn= mysqli_connect('localhost','root','','maindata');
if (!$conn) {
die('Connection failed: ' . mysqli_connect_error());
}
$sql = mysqli_query($conn,"SELECT * FROM accounts");
while($sql1=mysqli_fetch_array($sql,MYSQLI_ASSOC))
{
echo "<tr><td>".$sql1['ID']."</td>";
echo "<td>".$sql1['FullName']."</td>";
echo "<td>".$sql1['Purpose']."</td>";
echo "<td>".$sql1['Requestedby']."</td>";
echo "<td>".$sql1['DateRequested']."</td>";
echo "<td>".$sql1['DateInspected']."</td>";
echo "<td>".$sql1['DateSubmitted']."</td>";
echo "<td><a href='edit.php?id=".$sql1['ID']."'>Edit</a></td>";
echo "<td><a href='delete1.php?id=".$sql1['ID']."' class= 'action' name='Delete' onclick = return confirm('Are you sure you want to delete?');>Delete</a></td>";
}

?>

</tbody>
</table>


EDIT.PHP

<--PHP-->

//

$conn= mysqli_connect('localhost','root','','maindata');

if (!$conn) {

die('Connection failed: ' . mysqli_connect_error());

}


if (!$conn) {

die("Connection failed: " . mysqli_connect_error());


}

if (isset($_GET['ID']) && is_numeric($_GET['ID']))

{

$ID = $_GET['ID'];;

}

$sql = mysqli_query($conn, "SELECT* FROM accounts where ID = '$ID'");

while($row=mysqli_fetch_array($sql,MYSQLI_ASSOC)){

?>

<--HTML-->

<form action="update.php" method = "Get" class="form-horizontal">
<input type="text" name="id" value ="<?php echo $id; ?>">
<fieldset>
<legend>Account Information</legend>

<div>
<label class="control-label col-lg-4">First Name:</label>
<div class="col-lg-4">
<input type="text" name="fname" class="form-control" value="<?php echo $fname;?>" />
</div>
</div>

<div>
<label class="control-label col-lg-4">Middle Name:</label>
<div class="col-lg-4">
<input type="text" name="mname" class="form-control" value="<?php echo $mname; ?>"/>
</div>
</div>


problem:

if i click the edit button in ID number 2 on my table the value displaying in the text box is the value of the last row from my database table

Answer

first you need to determine unique parameter, in your case, its ID.

send that parameter to edit.php then use that parameter to fetch the data.

SELECT * FROM accounts where ID = $_POST['ID']
Comments