How to display data from database into textbox and update it in PHP?
source code:-
<?php
include('header.php');
include('config.php');
$id=$_REQUEST["id"]; //specify the id when you want to fetch data
$query="select * from product_master where id=$id";
$data=mysqli_query($conn,$query);
$res=mysqli_fetch_array($data);
$name=$res["product_name"];
$desc=$res["description"];
$image=$res["product_image"];
$cat=$res["category"];
$rate=$res["rate"];
if(isset($_POST["submit"]))
{
$file=$_FILES["file"]["name"];
$tmp=$_FILES["file"]["tmp_name"];
$ename=$_POST["name"];
$edesc=$_POST["desc"];
$ecat=$_POST["cat"];
$erate=$_POST["rate"];
$error=0;
if(empty($file))
{
$msg_file="enter product image";
$error=1;
}
if (empty($ename)) {
$msg_name="enter product name";
$error=1;
}
if (empty($edesc)) {
$msg_desc="enter product description";
$error=1;
}
if (empty($ecat)) {
$msg_cat="enter product category";
$error=1;
}
if (empty($erate)) {
$msg_rate="enter product rate";
$error=1;
}
if($error==0)
{
$folder="image/".$file;
move_uploaded_file($tmp,$folder);
echo $query="update product_master set product_name='$ename',description='$edesc',category='$ecat',rate=$erate,product_image='$folder' where id=$id";
$data=mysqli_query($conn,$query);
if($data)
{
$msg="update record successfully";
header('location:index.php');
}
else
{
$msg="not updated";
}
}
}
?>
<style type="text/css">
.msg{
margin-left:20px;
padding:5px;
font-size: 14px;
color: green;
}
</style>
<h1> edit product details</h1>
<div class="msg">
<?php if(isset($msg)){ echo $msg;} ?>
</div>
<form method="post" enctype="multipart/form-data">
<div class="input-box">
<label>product name</label>
<input type="text" class="input"name="name" id="name"placeholder="enter prduct name" value="<?php if(isset($name)){ echo $name; }?>">
<label class="msg"><?php if(isset($msg_name)){ echo $msg_name;} ?></label>
</div>
<div class="input-box">
<label>product description</label>
<textarea class="input" name="desc"><?php if(isset($desc)){ echo $desc; }?></textarea>
<label class="msg"><?php if(isset($msg_desc)){ echo $msg_desc;} ?></label>
</div>
<div class="input-box">
<label> category</label>
<select name="cat">
<option value="milk">milk</option>
<option value="rice">rice</option>
<option value="wheat">wheat</option>
</select>
<label class="msg"><?php if(isset($msg_cat)){ echo $msg_cat;} ?></label>
</div>
<div class="input-box">
<label>product image</label>
<input type="file" name="file" id="name" value="<?php if(isset($name)){ echo $name; }?>">
<label class="msg"><?php if(isset($msg_file)){ echo $msg_file;} ?></label>
</div>
<div class="input-box">
<label>product rate</label>
<input type="text" class="input"name="rate" id="name" value="<?php if(isset($rate)){ echo $rate; }?>">
<label class="msg"><?php if(isset($msg_rate)){ echo $msg_rate;} ?></label>
</div>
<div class="input-box">
<button type="submit" class="btn btn-primary" name="submit">update </button>
</div>
</form>
</body>
</html>
Post a Comment
If you have any doubts, Please let me know
Thanks!