Wednesday, December 25, 2024

Task 7: You had fetch the data on task 6 now you need to create a file name update.php and update is with new variable.

index.php

<html>
<body>
<?php
$conn=mysqli_connect('localhost','ksumanadhikari','welcome','task');
$sql='select * from data';
$result=mysqli_query($conn,$sql);
?>

<form action="submit.php" method="post">
    <input type="number" name="num">
    <button name="submit">Submit</button>
</form>
<?php
if(mysqli_num_rows($result)>0){
?>
<table border="1">
<tr>
<th>sn</th>
<th>num</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['sn'];?></td>
<td><?php echo $row['num'];?></td>
<td><a href='edit.php?sn=<?php echo $row['sn'];?>'>Edit</a></td>
<td><a href='delete.php?sn=<?php echo $row['sn'];?>'>delete</a></td>
</tr>


<?php } ?>
</table>
<?php }else {
    echo "no table found";
    mysqli_close($conn);
    } ?>
</body>
</html>

submit.php

<?php
$conn=mysqli_connect('localhost','ksumanadhikari','welcome','task') or die("margaya madarchood! la la lalalalalalala la la");
$num=$_POST['num'];
$sql="insert into data(num) values('$num')";
mysqli_query($conn,$sql);
header("location: index.php");
exit();
?>

edit.php

<?php
$conn=mysqli_connect('localhost','ksumanadhikari','welcome','task') or die("connection failed");
$sn=$_GET['sn'];
$sql="select * from data where sn='$sn'";
$result=mysqli_query($conn,$sql);

if(mysqli_num_rows($result)>0){
    while($row=mysqli_fetch_assoc($result)) {
?>
<form action="update.php" method="post">
 <input type="hidden" name="sn" value="<?php echo $row['sn']; ?>" />
Change Number <input type="number" name="num" value="<?php echo $row['num']; ?>" />
<input type="submit" value="Update Data">
</form>
<?php } } ?>



update.php

<?php
$conn=mysqli_connect('localhost','ksumanadhikari','welcome','task') or die("Connection Failed");

$sn=$_POST['sn'];
$num=$_POST['num'];

$sql = "UPDATE data SET num = $num WHERE sn = $sn";  
mysqli_query($conn, $sql);  

mysqli_close($conn);

header("location: index.php");
exit();
?>

 


0 comments:

Post a Comment

Copyright © Web Devloper LogBook | Powered By Blogger | Blogger Templates
Design by Saeed Salam | Blogger Theme by NewBloggerThemes.com