php课程---练习(发布新闻)
做一个发布新闻的页面,实现发布新闻,查看新闻,修改新闻与删除等功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div> <br /> <form id="biaodan" style="width:50%" action="fabuxinwen.php" method="post"> <div style="text-align:center">发布新闻</div> <div style="visibility:hidden"><span>编号:</span><input type="text" name="newsid"/></div> <div><span>标题:</span><input type="text" name="title" style="width:300px" /></div><br /> <div><span>作者:</span><input type="text" name="author" /></div><br /> <div><span>来源:</span><input type="text" name="source" /></div><br /> <div> <table><tr><td>内容:</td><td><textarea rows="5" style="width:450px" name="content"></textarea></td></tr></table><!--通过table实现内容的垂直居中 --> </div> <div style="visibility:hidden"><span>时间:</span><input type="text" name="times" /></div> <div style="text-align:center"><input type="submit" value="提交" name="submit" /> <input type="button" value="查看" id="search" onclick="window.open('chakan.php')"/> </div> </form> </div> <?php @$submit=$_POST["submit"]; if($submit!=null) { $id=$_POST["newsid"]; $title=$_POST["title"]; $author=$_POST["author"]; $source=$_POST["source"]; $content=$_POST["content"]; $times=$_POST["times"]; $date=date("Y-m-d H-i-s"); $db=new mysqli("localhost","root","","newssystem"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="insert into news values('{$id}','{$title}','{$author}','{$source}','{$content}','{$date}' )"; } $result=$db->query($sql); if($result) { header("Location:fabuxinwen.php"); } else { echo "添加失败"; } } ?> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php $db=new mysqli("localhost","root","","newssystem"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="select * from news"; $result=$db->query($sql); echo "<table width=80% border='1' cellpadding='0' cellspacing='0'>"; echo "<tr align='center'><td>id</td> <td>title</td> <td>author</td> <td>source</td><td>times</td> <td>update</td> <td>delete</td> </tr>"; while($row=$result->fetch_row()) { echo "<tr bgcolor='#00CC00' align='center'><td>{$row[0]}</td> <td>{$row[1]}</td> <td>{$row[2]}</td> <td>{$row[3]}</td> <td>{$row[5]}</td> <td><a href='xiugai.php?code=".$row[0]."'>update</a></td> <td><a href='shanchu.php?code=".$row[0]."'>delete</a></td> </tr>"; } echo "</table>"; } ?> <a href="fabuxinwen.php">返回</a> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php $id=$_GET["code"]; $db=new mysqli("localhost","root","","newssystem"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="delete from news where newsid='".$id."'"; $result=$db->query($sql); if($result) { header("Location:chakan.php"); } else { echo "删除失败"; } } ?> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php $id=$_GET["code"]; $db=new mysqli("localhost","root","","newssystem"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="select * from news where newsid='".$id."'"; $result=$db->query($sql); $row=$result->fetch_row(); } ?> <br /> <form id="biaodan" style="width:50%" action="xiugaichuli.php" method="post"> <div style="text-align:center">修改新闻</div> <div style="visibility:hidden"><span>编号:</span><input type="text" name="newsid" value="<?php echo $row[0] ?>" /></div> <div><span>标题:</span><input type="text" name="title" style="width:300px" value="<?php echo $row[1] ?>" /></div><br /> <div><span>作者:</span><input type="text" name="author" value="<?php echo $row[2] ?>" /></div><br /> <div><span>来源:</span><input type="text" name="source" value="<?php echo $row[3] ?>" /></div><br /> <div> <table><tr><td>内容:</td><td><textarea rows="5" style="width:450px" name="content" ><?php echo "$row[4]"?></textarea></td></tr></table><!--通过table实现内容的垂直居中 --> </div> <div style="visibility:hidden"><span>时间:</span><input type="text" name="times" /></div> <div style="text-align:center"><input type="submit" value="提交" name="submit" /> <input type="button" value="查看" id="search" onclick="window.open('chakan.php')"/> </div> </form> </div> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php $id=$_POST["newsid"]; $title=$_POST["title"]; $author=$_POST["author"]; $source=$_POST["source"]; $content=$_POST["content"]; $times=$_POST["times"]; $date=date("Y-m-d H-i-s"); $db= new mysqli("localhost","root","","newssystem"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="update news set title='".$title."',author='".$author."',source='".$source."',content='".$content."',times='".$date."' where newsid='".$id."'"; $result=$db->query($sql); if($result) { header("Location:chakan.php"); } else { echo "修改失败";} } ?> </body> </html>
发布新闻这一段代码可以实现新闻内测功能