Steffi Matchado Steffi Matchado - 6 months ago 10
MySQL Question

Updating info into mysql using php

I am trying to update my database by getting the id selected by user.

<form action="edit1.php" method="post">
<?php
$query = "SELECT * FROM disease1;";
$result = mysqli_query($dp, $query);
echo "<table border=5>
<tr>
<th>Disease ID</th>
<th>Disease</th>
<th>Sub Disease</th>
<th>Associated Disease</th>
<th>Edit</th>
</tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row{'id'}."</td>";
echo "<td>".$row{'Disease'}."</td>";
echo "<td>".$row{'SubDisease'}."</td>";
echo "<td>".$row{'Associated_Disease'}."</td>";
echo "<td><input type='radio' name='id' value='".$row[id]."'></td>";
echo "</tr>";}
echo "</table>";
?> <div>
<input type = 'submit' value = 'Update' name = 'submitupdate'>


My Edting page edit1.php

<?php
$conn = mysqli_connect('localhost','root','','tool')
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
$query = "SELECT * FROM disease where id=".$_POST["id"];
$result = mysqli_query($conn, $query);
$count= mysqli_num_rows($result);
echo $count;
?>
<form action="update.php" method="post">
<input type="hidden" value="<?php echo $row['id'];?>" name="id"/>
Diease<input type="text" name="Disease" value="<?php echo $row['Disease'];? >"/>
SubDisease<input type="text" name="SubDisease" value="<?php echo $row['SubDisease'];?>"/>
Associated Disease<input type="text" name="Associated_Disease" value="<?php echo $row['Associated_Disease'];?>"/>
<input type="submit" value="update">
</form>


My update.php

<?php
$conn = mysqli_connect('localhost','root','','tool');
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
$disease=$_POST['Disease'];
$SubDisease=$_POST['SubDisease'];
$Associated_Disease= $_POST['Associated_Disease'];
$id = $_POST ['id'];
$update="Update disease1 set Disease='".$disease."', SubDisease='".$SubDisease."', Associated_Disease='".$Associated_Disease."' where id=".$_POST["id"];
mysqli_query($conn,$update);
?>


But the id is not reading and the value of the id is not passed to the update command. Can any one help me with this

Answer

You need to fetch data from your query result

$row = mysqli_fetch_assoc($result);

Then assign to form.

Complete code would be

$query = "SELECT * FROM disease where id=".$_POST["id"];
$result = mysqli_query($conn, $query);
$count= mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);

<form action="update.php" method="post">
    <input type="hidden" value="<?php echo $row['id'];?>" name="id"/>
    Diease<input type="text" name="Disease" value="<?php echo $row['Disease'];?    >"/>
    SubDisease<input type="text" name="SubDisease" value="<?php echo $row['SubDisease'];?>"/>
    Associated Disease<input type="text" name="Associated_Disease" value="<?php echo $row['Associated_Disease'];?>"/>
    <input type="submit" value="update">
    </form>
Comments