亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Implementation of modification function of news management system developed with PHP (Part 2)

In the previous section, we talked about querying in the database and displaying the code. Now let’s continue to explain the modification function. First, review the complete source code of the previous lesson.

<?php
    header("Content-type: text/html; charset=utf-8");//設(shè)置編碼
    $con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫(kù)連接失敗");
    mysql_select_db('news') or die("指定的數(shù)據(jù)庫(kù)不能打開(kāi)");
    mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫(kù)的字符集
    $id=$_GET['id'];
    $sql="select * from new where id=$id";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
    *{margin:0px;padding:0px;}
    body{background:#ccc;}
    .add{width:450px;height:280px;background:#eee;float:left;}
    .cont{width:500px;height:350px;margin-top:5px;margin-left:5px;}
    form{margin-left:10px;padding-top:30px;}
    .sub{width:100px;height:40px;border:1px solid #ccc;}
    .sub:hover{background:#f90}
    </style>
</head>
<body>
    <div class="add">
        <div class="cont">
            <form method="post" action="modify.php?id=<?php echo $id;?>">
                標(biāo)題:<input type="text" name="title" value="<?php echo $row['title']?>"></br></br>
                內(nèi)容:<textarea cols="50" rows="5" name="content"><?php echo $row['content']?></textarea></br></br>
                <input type="submit" value="修改" class="sub">
            </form>
        </div>
    </div>
</body>
</html>

The above code and form Submit it to the file modify.php. Next, let’s take a look at this file

First we need to connect to the database

 <?php
    header("Content-type: text/html; charset=utf-8");//設(shè)置編碼
    $con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫(kù)連接失敗");
    mysql_select_db('news') or die("指定的數(shù)據(jù)庫(kù)不能打開(kāi)");
    mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫(kù)的字符集

Then get the form information

 <?php 
    $id = $_GET['id'];
    $title = $_POST['title'];
    $content = $_POST['content'];
    $messtime = time();

Note: here We also need to get the id. In the previous lesson, we got the id and then queried it in the database to query the information. Here we need conditions during the modification process, such as modifying that piece of information. It will be very easy for us to use the id here. Convenient

Next write the modification statement

$sql = "update new set title='$title',content='$content',messtime='$messtime' where id='$ id'";

$res = mysql_query($sql);

Judge the modified statement after writing it

if($res){
echo "<script>alert('Modification successful');location.href='newlist.php';</script>";
}else{
?echo "<script>alert('Modification failed ');history.go(-1);</script>";
}

The complete code is as follows:

<?php
	header("Content-type: text/html; charset=utf-8");//設(shè)置編碼
	$con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫(kù)連接失敗");
	mysql_select_db('news') or die("指定的數(shù)據(jù)庫(kù)不能打開(kāi)");
	mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫(kù)的字符集

	$id = $_GET['id'];
	$title = $_POST['title'];
	$content = $_POST['content'];
	$messtime = time();

	$sql = "update new set title='$title',content='$content',messtime='$messtime' where id='$id'";
	$res = mysql_query($sql);
	if($res){
		echo "<script>alert('修改成功');location.href='newlist.php';</script>";
	}else{
		echo "<script>alert('修改失敗');history.go(-1);</script>";
	}
?>

In this way, we have completed the modification of a simple function

Continuing Learning
||
submitReset Code