PHP develops simple news release system and implements news modification page function
The previous section explained the news modification page of a simple news release system developed with PHP and clicking "Modify" from the news list page
will jump directly to the news modification page and display the content.
This section explains how to implement the editing and modification function of the news modification page through PHP code.
First of all, we need to connect to the database test and table new:
<?php $link = mysqli_connect('localhost','uesename','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("連接失敗:".mysqli_connect_error()); } ?>
Use the POST method to obtain the value. Here we need to update three items: title title, author author, news content content
<?php $id = isset($_POST['id'])?$_POST['id']:""; //獲取id的值 $title = isset($_POST['title'])?$_POST['title']:""; $author = isset($_POST['author'])?$_POST['author']:""; $content = isset($_POST['content'])?$_POST['content']:""; ?>
Use update in SQL statement: update data
<?php $sql="update new set title = '$title',author = '$author',content = '$content' where id = '$id'"; //echo $sql; $rel=mysqli_query($link,$sql);//執(zhí)行sql語句 //echo $rel ?>
So that we can achieve complete modification function
Complete update.php code:
<?php header("content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("連接失敗:".mysqli_connect_error()); } $id = isset($_POST['id'])?$_POST['id']:""; $title = isset($_POST['title'])?$_POST['title']:""; $author = isset($_POST['author'])?$_POST['author']:""; $content = isset($_POST['content'])?$_POST['content']:""; $sql="update new set title = '$title',author = '$author',content = '$content' where id = '$id'"; //echo $sql; $rel=mysqli_query($link,$sql);//執(zhí)行sql語句 //echo $rel if($rel){ echo "<script>alert('新聞修改成功');window.location.href='list.php'</script>"; }else{ echo "<script>alert('新聞修改失敗');window.location.href='edit.php'</script>"; } ?>
At this point, our simple news release system developed in PHP has been fully introduced. Friends can learn it through The code pages in this chapter are used together to realize the addition, deletion, modification, paging, and search functions of a complete simple news release system.
Note: The course in this chapter is just a simple demonstration. The code is for learning reference only and cannot be used directly in projects.