我创建了一个每行都有一个编辑按钮的表
ID Name Age Class EDIT
1 sam 12 12th edit
用于编辑按钮的代码是
echo "<td><a href=\"admin_edit_members.php?id=".$row['id']."\">Edit</a></td>";
admin_edit_members.php页面上使用的代码是:
<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);
?>
我的问题是,当我编辑表单时,我需要编辑所有字段或在数据库中输入的空白值。有没有办法让用户只能编辑他想要的特定字段
答案 0 :(得分:0)
运行选择查询
$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;
}
现在运行更新查询
答案 1 :(得分:0)
请尝试以下方法: -
<?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>