How to implement data modification function

We created the modification page in the previous section edit.php, in this section we will implement the modification function

35.png

is similar to the delete function , we need to obtain the id of the information that needs to be modified Query all the information of this id in the database through SQL statements. Then modify the information of this id through SQL statements

Create the update.php file to implement the modification function.

Write the query statement on the edit.php page

<?php
$id = isset($_GET["id"])?$_GET["id"]:"";
$title = isset($_POST['title'])?$_POST['title']:"";
$name = isset($_POST['name'])?$_POST['name']:"";
$video = isset($_POST['video'])?$_POST['video']:"";

$sql = "select id,title,name,video from list where id = '$id'";
$result = mysqli_query($link,$sql);
$rel = mysqli_fetch_array($result);
?>

Display in the html code:

You need to use the hidden domain here type="hidden " Get the id.

Add the following statement in the <form> form:

<form method="post" class="form-x" action="update.php" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $rel["id"]?>">
</form>

Make the following changes to the title, video content name, and description. The css style can be customized Adjust as needed:

<div class="form-group">
  <div class="label">
    <label>标题:</label>
  </div>
  <div class="field">
    <input type="text" class="input w50" value="<?php echo $rel["title"]?>" name="title" data-validate="required:请输入标题" />
    <div class="tips"></div>
  </div>
</div>
<div class="form-group">
  <div class="label">
    <label>视频:</label>
  </div>
  <div class="field">
    <input type="text" class="input w50" value="<?php echo $rel["video"]?>" name="video" data-validate="required:请输入视频名称" />
    <input type="submit" name="upload" class="button bg-blue margin-left" id="image1" value="+ 浏览上传"  style="float:left;">
    <div class="tips"></div>
  </div>
</div>
<div class="form-group">
  <div class="label">
    <label>描述:</label>
  </div>
  <div class="field">
    <textarea class="input" name="name" style=" width:400px;height:200px;"><?php echo $rel["name"]?></textarea>
    <div class="tips"></div>
  </div>
</div>

Of course, make the following modifications in list.php. $rows["id"] has the same while loop output as the delete function.

<div class="button-group">
    <a class="button border-main" href="edit.php?id=<?php echo $rows["id"]?>"><span class="icon-edit"></span>修 改</a>
    <a class="button border-red" href="delete.php?id=<?php echo $rows["id"]?>" onclick="return del(1,1,1)">
        <span class="icon-trash-o"></span>删 除
    </a>
</div>

In this way, you can modify the function code in the update.php file and modify the information of this ID in the database through SQL statements.

<?php
header("content-type:text/html;charset=utf-8");
include("config.php"); //引入数据库公共文件
$id = isset($_POST["id"])?$_POST["id"]:"";
$title = isset($_POST['title'])?$_POST['title']:"";
$name = isset($_POST['name'])?$_POST['name']:"";
$video = isset($_POST['video'])?$_POST['video']:"";

$sql="update list set title='$title',name='$name',video='$video' where id='$id'";
//echo $sql;
$rel=mysqli_query($link,$sql);//执行sql语句
//echo $rel

if($rel){
   echo "<script>alert('修改成功');window.location.href='list.php'</script>";
}else{
   echo "<script>alert('修改失败');window.location.href='edit.php'</script>";
}
?>


Continuing Learning
||
<?php header("content-type:text/html;charset=utf-8"); include("config.php"); //引入数据库公共文件 $id = isset($_POST["id"])?$_POST["id"]:""; $title = isset($_POST['title'])?$_POST['title']:""; $name = isset($_POST['name'])?$_POST['name']:""; $video = isset($_POST['video'])?$_POST['video']:""; $sql="update list set title='$title',name='$name',video='$video' where id='$id'"; //echo $sql; $rel=mysqli_query($link,$sql);//执行sql语句 //echo $rel if($rel){ echo "<script>alert('修改成功');window.location.href='list.php'</script>"; }else{ //echo "<script>alert('修改失败');window.location.href='edit.php'</script>"; } ?>
submitReset Code