建立数据库,建表,对新闻实现添加,修改和删除功能,是实例化应用
主界面:
<body> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>id</td> <td>title</td> <td>author</td> <td>source</td> <td>date</td> <td>update</td> <td>delete</td> </tr> <?php $db = new MySQLi("localhost","root","","newssystem"); !mysqli_connect_error() or die("连接失败"); $sql = "select * from news"; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[5]}</td> <td><a href=‘xiugai.php?newsid={$v[0]}‘>update</a></td> <td><a href=‘shanchu.php?newsid={$v[0]}‘}>delete</a></td> </tr>"; } ?> </table>
</body>
新闻发布页面:
<body> <h1>发布新闻</h1> <form action="tijiao.php" method="post"> <div>标题:<input type="text" name="title" /></div> <div>作者:<input type="text" name="author" /></div> <div>来源:<input type="text" name="source" /></div> <div>内容:<textarea name="content" cols="100" rows="10" ></textarea></div> <div>时间:<input type="date" name="time" /></div> <div><input type="submit" value="提交"/><a href=‘chakan.php‘><input type="button" value="查看"></a></div> </form> </body>
数据处理:
<?php $title = $_POST["title"]; $author = $_POST["author"]; $source = $_POST["source"]; $content = $_POST["content"]; $time = $_POST["time"]; $db = new MySQLi("localhost","root","","newssystem"); !mysqli_connect_error() or die("连接失败"); $sql = "insert into news values(‘‘,‘{$title}‘,‘{$author}‘,‘{$source}‘,‘{$content}‘,‘{$time}‘)"; $result = $db->query($sql); if($result) { header("location:xwmain.php"); } else { echo "添加失败"; }
修改页面:
<body> <h1>修改新闻</h1> <?php $newsid = $_GET["newsid"]; $db = new MySQLi("localhost","root","","newssystem"); !mysqli_connect_error() or die("连接失败"); $sql = "select * from news where newsid=‘{$newsid}‘"; $result = $db->query($sql); $attr = $result->fetch_row(); ?> <form action="xiugai1.php" method="post"> <div><input type="hidden" name="newsid" value="<?php echo $attr[0] ?>"/></div> <div>标题:<input type="text" name="title" value="<?php echo $attr[1] ?>"/></div> <div>作者:<input type="text" name="author" value="<?php echo $attr[2] ?>"/></div> <div>来源:<input type="text" name="source" value="<?php echo $attr[3] ?>"/></div> <div>内容:<textarea name="content" cols="100" rows="10" value="<?php echo $attr[4] ?>"></textarea></div> <div>时间:<input type="text" name="time" value="<?php echo $attr[5] ?>"/></div> <div><input type="submit" value="修改"/><a href=‘chakan.php‘><input type="button" value="查看"></a></div> </form> </body>
数据处理页面;
<?php $newsid = $_POST["newsid"]; $title = $_POST["title"]; $author = $_POST["author"]; $source = $_POST["source"]; $content = $_POST["content"]; $time = $_POST["time"]; $db = new MySQLi("localhost","root","","newssystem"); !mysqli_connect_error() or die("连接失败"); $sql = "update news set title = ‘{$title}‘,author = ‘{$author}‘,source = ‘{$source}‘,content = ‘{$content}‘,time = ‘{$time}‘ where newsid = ‘{$newsid}‘"; $result = $db->query($sql); if($result) { header("location:chakan.php"); } else { echo "修改失败"; }
删除页面:
<?php $newsid = $_GET["newsid"]; $db = new MySQLi("localhost","root","","newssystem"); !mysqli_connect_error() or die("连接失败"); $sql = "delete from news where newsid=‘{$newsid}‘"; $result = $db->query($sql); if($result) { header("location:chakan.php"); } else { echo "删除失败!"; }
原文:http://www.cnblogs.com/dongqiaozhi/p/5472161.html