PHP习题新闻增删改

首先写入一个添加新闻的代码:

<body>
<h2>发布新闻</h2>
<form action="tianjiachuli.php" method="post">
<div>标题:<input type="text" name="biaoti" /></div>
<br />
<div>作者:<input type="text" name="zuozhe" /></div>
<br />
<div>来源:<input type="text" name="laiyuan" /></div><br />
<div>内容:<textarea cols="30" rows="5" name="neirong"></textarea></div>
<input align="middle" type="submit" value="提交" />
</form>
<a href="xinwenbiaoge.php"><input type="submit" value="查看" /></a>
</body>

然后点击查看后转换到新闻表格的页面

<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>newsid</td>
<td>title</td>
<td>Author</td>
<td>source</td>
<td>content</td>
<td>time</td>
<td>delete</td>
<td>upadte</td>
</tr>
<?php

$db = new MySQLi("localhost","root","123","newssystem");

$sql = "select * from news";

$result = $db->query($sql);


while($attr = $result->fetch_row())
{
    echo "<tr>
                <td>$attr[0]</td>
                <td>$attr[1]</td>
                <td>$attr[2]</td>
                <td>$attr[3]</td>
                <td>$attr[4]</td>
                <td>$attr[5]</td>
                <td><a href='xinwenshanchu.php?newsid={$attr[0]}'>删除</a></td>
                <td><a href='xinwenxiugai.php?newsid={$attr[0]}'>修改</a></td>
        </tr>"    ;
}
?>
</table>
</body>

 然后点击修改后转换到修改页面

<body>
<h2>新闻修改</h2>
<?php
$code = $_GET["newsid"];

$db = new MySQLi("localhost","root","123","newssystem");

$sql = "select * from news where newsid='{$code}'";

$result = $db->query($sql);

$attr = $result->fetch_row();

?>
<form action="xinwenxiugaichuli.php" method="post">
<div><input  type="hidden" name="id" value="<?php echo $code ?>" /></div>
<div>标题:<input type="text" name="biaoti" value="<?php echo $attr[1]?>" /></div>
<br />
<div>作者:<input type="text" name="zuozhe"  value="<?php echo $attr[2] ?>" /></div>
<br />
<div>来源:<input type="text" name="laiyuan"  value="<?php echo $attr[3] ?>" /></div><br />
<div>内容:<textarea cols="30" rows="5" name="neirong"  value="<?php echo $attr[2] ?>" ></textarea></div>
<input align="middle" type="submit" value="提交" />
</form>
<a href="xinwenbiaoge.php"><input type="submit" value="查看" /></a>
</body>

然后制作一个处理修改的代码

<?php
$id = $_POST["id"];
$biaoti = $_POST["biaoti"];
$zuozhe = $_POST["zuozhe"];
$laiyuan = $_POST["laiyuan"];
$neirong = $_POST["neirong"];

$db = new MySQLi("localhost","root","123","newssystem");

$sql = "update news set title='{$biaoti}',Author='{$zuozhe}',source='{$laiyuan}',content='{$neirong}' where newsid='{$id}' ";

$r = $db->query($sql);

echo $sql;

if($r)
{
        header("location:xinwenxiugai.php");
}
else
{
    echo "修改失败";    
}
?>
原文地址:https://www.cnblogs.com/qz1234/p/5590321.html