PHP开发简单新闻发布系统之新闻修改页面

前面我们介绍了新闻列表页的实现及其部分功能模块

1607.png

当我们在新闻列表页的编辑选项点击“修改”时,直接跳转到新闻修改页面,

<body>
<a href="edit.php?id=<?php echo $arr['id']?>"><font color="red">修改</font></a>
</body>

这里新闻修改页面为edit.php,点击“修改”时通过本条新闻的id值跳转到新闻修改页,

在新闻修改页使用$_GET获取id,通过数据库select语句把内容显示在修改页的<form>表单中修改。

<?php
$id = isset($_GET["id"])?$_GET["id"]:"";
?>

SQL语句如下:

<?php
$sql="select id,title,author,content from new where id = '$id'";

$rel = mysqli_query($link,$sql);//执行sql语句

$arr= mysqli_fetch_array($rel); //获取一条新闻的所有信息
?>

通过POST方式获取标题,作者和内容

<?php
$title = isset($_POST['title'])?$_POST['title']:"";    //获取标题

$author = isset($_POST['author'])?$_POST['author']:"";    //获取作者

$content = isset($_POST['content'])?$_POST['content']:"";    //获取内容
?>

把获取的内容显示在HTML页面中

<body>
<form name="article" method="post" action="update.php" style="margin:5px;">
  <h1>新闻修改页</h1>
  <input type="hidden" name="id" value="<?php echo $arr['id']?>"/><br/>
  标 题:<input type="text" name="title" value="<?php echo $arr['title']?>"/><br/><br/>
  作 者:<input type="text" name="author" value="<?php echo $arr['title']?>"/><br/><br/>
  <span>内 容:</span>
  <textarea cols=30 rows=5 name="content"><?php echo $arr['content']?></textarea><br/><br/>
  <input type="submit" value="修改新闻"/>
</form>
</body>

这样就可以实现点击“修改”后跳转到新闻编辑页并显示新闻内容的HTML页面。


完整代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>新闻修改页面</title>
  <style type="text/css">
    span{display:inline-block; float: left; width: 50px;}
    input[type="submit"]{margin-left: 10%;}
  </style>
</head>
<body bgcolor="#ccc">
  <?php
  
  $link = mysqli_connect('localhost','username','password','test');
          mysqli_set_charset($link, "utf8");
  if (!$link) {
    die("连接失败:".mysqli_connect_error());
  }
  
  $id = isset($_GET["id"])?$_GET["id"]:"";
  $title = isset($_POST['title'])?$_POST['title']:"";
  $author = isset($_POST['author'])?$_POST['author']:"";
  $content = isset($_POST['content'])?$_POST['content']:"";
  
  $sql="select id,title,author,content from new where id = '$id'";
  //echo $sql;
  $rel = mysqli_query($link,$sql);//执行sql语句
  $arr= mysqli_fetch_array($rel);
  ?>
  
  <form name="article" method="post" action="update.php" style="margin:5px;">
    <h1>新闻修改页</h1>
    <input type="hidden" name="id" value="<?php echo $arr['id']?>"/><br/>
    标 题:<input type="text" name="title" value="<?php echo $arr['title']?>"/><br/><br/>
    作 者:<input type="text" name="author" value="<?php echo $arr['title']?>"/><br/><br/>
    <span>内 容:</span>
    <textarea cols=30 rows=5 name="content"><?php echo $arr['content']?></textarea><br/><br/>
    <input type="submit" value="修改新闻"/>
  </form>
</body>
</html>


继续学习
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>新闻修改页面</title> <style type="text/css"> span{display:inline-block; float: left; width: 50px;} input[type="submit"]{margin-left: 10%;} </style> </head> <body bgcolor="#ccc"> <?php $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } $id = isset($_GET["id"])?$_GET["id"]:""; $title = isset($_POST['title'])?$_POST['title']:""; $author = isset($_POST['author'])?$_POST['author']:""; $content = isset($_POST['content'])?$_POST['content']:""; $sql="select id,title,author,content from new where id = '$id'"; //echo $sql; $rel = mysqli_query($link,$sql);//执行sql语句 $arr= mysqli_fetch_array($rel); ?> <form name="article" method="post" action="update.php" style="margin:5px 500px;"> <h1>新闻修改页</h1> <input type="hidden" name="id" value="<?php echo $arr['id']?>"/><br/> 标 题:<input type="text" name="title" value="<?php echo $arr['title']?>"/><br/><br/> 作 者:<input type="text" name="author" value="<?php echo $arr['title']?>"/><br/><br/> <span>内 容:</span> <textarea cols=30 rows=5 name="content"><?php echo $arr['content']?></textarea><br/><br/> <input type="submit" value="修改新闻"/> </form> </body> </html>
提交重置代码