How to implement the function of modifying and deleting categories
In the previous chapter, we created a cateadd.php file to implement the category addition page. Modify this file and create a cateedit.php file to display the modification page. After modification, it becomes the following form:
Let’s return to the original cate.php page , in the previous list page, until we modify or delete a certain piece of data, we obtain the id of the information that needs to be modified or deleted and modify or delete the information of this id through SQL statements.
Make the following modifications to the "modify" and "delete" items in cate.php to obtain the id value.
<td> <div class="button-group"> <a class="button border-main" href="cateedit.php?id=<?php echo $val['id'];?>"> <span class="icon-edit"></span> 修 改</a> <a class="button border-red" href="catedel.php?id=<?php echo $val['id'];?>" onclick="return del(1,2)"> <span class="icon-trash-o"></span> 删 除</a> </div> </td>
First perform the deletion operation, create the catedel.php file to implement the deletion function, delete the obtained classification id, and use SQL statements to delete the classification information in the database table.
<?php header("content-type:text/html;charset=utf-8"); include("config.php"); $id = isset($_GET['id'])?$_GET['id']:""; $sql = "delete from cate where id = '$id'"; //echo $sql; $rel = mysqli_query($link,$sql); if($rel){ echo "<script>alert('删除成功');window.location.href='cate.php'</script>"; }else{ echo "<script>alert('删除失败');window.location.href='cate.php'</script>"; } ?>
Perform the modification operation again. The cateedit.php modification file has been created previously. The difference from "delete" is that the id of the information that needs to be modified is obtained and all the information of this id in the database is queried through SQL statements. . Then modify the information of this ID through SQL statements. In the cateedit.php page, write the query statement
<?php header("content-type:text/html;charset=utf-8"); include("config.php"); $id = isset($_GET["id"])?$_GET["id"]:""; $cate_name = isset($_POST["cate_name"])?$_POST["cate_name"]:""; $rank = isset($_POST["rank"])?$_POST["rank"]:""; $sql = "select id,cate_name,rank from cate where id = '$id'"; $result = mysqli_query($link,$sql); $rel = mysqli_fetch_array($result); ?>
You also need to use the hidden field type="hidden" to obtain the id. Add the following statement in the <form> form:
<input type="hidden" name="id" value="<?php echo $rel["id"]?>">
To obtain various information of the classification, you need to make the following modifications in the static page:
<div class="field"> <input type="text" class="input w50" name="cate_name" value="<?php echo $rel['cate_name'];?>"/> <div class="tips"></div> </div>
<div class="field"> <input type="text" class="input w50" name="rank" value="<?php echo $rel["rank"];?>" data-validate="number:分类级别必须为数字" /> <div class="tips"></div> </div>
Create the cateupdate.php file to implement the modification function. POST receives the passed id data and uses the SQL statement UPDATE SET statement to implement content modification.
<?php header("content-type:text/html;charset=utf-8"); include("config.php"); $id = isset($_POST["id"])?$_POST["id"]:""; $cate_name = isset($_POST["cate_name"])?$_POST["cate_name"]:""; $rank = isset($_POST["rank"])?$_POST["rank"]:""; $sql="update cate set cate_name='$cate_name',rank='$rank'where id='$id'"; //echo $sql; $rel=mysqli_query($link,$sql);//执行sql语句 //echo $rel if($rel){ echo "<script>alert('修改成功');window.location.href='cate.php'</script>"; }else{ echo "<script>alert('修改失败');window.location.href='cateedit.php'</script>"; } ?>