i have created a table that has a edit button on each row
ID Name Age Class EDIT
1 sam 12 12th edit
the code used for the edit button is
echo "<td><a href=\"admin_edit_members.php?id=".$row['id']."\">Edit</a></td>";
Code used on admin_edit_members.php page is:
<form class="form-horizontal" role="form" action="<?php $_PHP_SELF ?>" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-lg-3 control-label">Name:</label>
<div class="col-lg-8">
<input class="form-control" value="" type="text" name="name">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Age:</label>
<div class="col-lg-8">
<input class="form-control" value="" type="text" name="age">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Class:</label>
<div class="col-lg-8">
<input class="form-control" value="" type="text" name="class">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit" name="submit">
<span></span>
</div>
</div>
</form>
<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$id = $_GET['id'];
$name = mysqli_real_escape_string($con, $_POST['name']);
$age = mysqli_real_escape_string($con, $_POST['age']);
$class = mysqli_real_escape_string($con, $_POST['class']);
$sql = "UPDATE student SET name='".$name."',age='".$age."',class='".$class."' WHERE id ='".$id."'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
header("Location: student_list.php");
exit;
mysqli_close($con);
?>
my problem is that when i edit the form i need to edit all the fields or else a blank value entered in the database. is there a way where the user can edit only spefic fields he wants
Run the select query
$query="SELECT * FROM student";
$result= mysqli_query($con, $query) or die(mysqli_error());
//get the value from database
while ($row= mysqli_fetch_array($result))
{
$name_data=$row['name'];
$name_age=$row['age'];
}
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$age=$_POST['age'];
if(empty($name))
{
//if the value is empty its going to set it equal to the database value
$name=$name_data;
}
else
$name=$name;
if(empty($age))
{
$age=$name_age;
}
else
$age=$age;
}
now Run your update query
Try below :-
<?php
$sql = "select * from student where id = $_GET['id']";
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_array($res);
?>
<form class="form-horizontal" role="form" action="<?php $_PHP_SELF ?>" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-lg-3 control-label">Name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row["name"]; ?>" type="text" name="name" >
</div>
</div>
//likewise in all inputbox.
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.