간단한 블로그 시스템, 게시, 편집, 삭제, 데이터베이스 연습~ 보안은 일단 무시하고 먼저 GIF 애니메이션을 살펴보겠습니다.
데이터베이스 스크린샷 보기:
자, 코드를 보세요:
conn.php:
<?php /** * mysql_query — 发送一条 MySQL 查询 */ /** * 连接数据库(返回资源) */ @mysql_connect("127.0.0.1:3306","root","") or die("mysql数据库连接失败"); /** * 选择一个数据库作为默认的数据库使用 */ @mysql_select_db("blog")or die("db连接失败");
index.php :
아아앙
add.php:
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <br><br> <form action="" method="get" style='align:"right"'> <input type="text" name="keys" > <input type="submit" name="subs" > </form> <hr> <?php include("conn.php"); //引入连接数据库 $sql = "select id,hits,title,date,contents from simpleblog"; $result = mysql_query($sql); // 只要放一个正确的sql就可以执行 while (list($id, $hits, $title, $date, $contents) = mysql_fetch_row($result)){ // 遍历表内容 echo "id = {$id}<br>"; echo "hits = {$hits}<br>"; echo "title = {$title}<br>"; ?>titleLink: <a href="view.php?id=<?php echo $id; ?>"><?php echo $title."<br>"; ?></a><?php echo "date = {$date}<br>"; echo "contents = {$contents}<br>"; echo "contents = ".iconv_substr($contents,0,15)."...<br>"; // iconv_substr — 截取字符串的部分 ?>edit: <a href="edit.php?id=<?php echo $id; ?>">edit</a><?php echo "<br>"; ?>delete: <a href="del.php?id=<?php echo $id; ?>">delete</a><?php echo "<br>"; echo "--------------------------------------------------<br>"; }
view.php:
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入连接数据库 if (!empty($_POST['sub'])) { $title = $_POST['title']; //获取title表单内容 $con = $_POST['con']; //获取contents表单内容 $sql= "insert into simpleblog values(null,'0','$title',now(),'$con')"; mysql_query($sql); echo "insert success!"; } ?> <form action="add.php" method="post"> title :<br> <input type="text" name="title"><br><br> contents:<br> <textarea rows="5" cols="50" name="con"></textarea><br><br> <input type="submit" name="sub" value="submit"> </form>
edit.php:
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入连接数据库 $id = $_GET['id']; $sql = "select id,hits,title,date,contents from simpleblog where id='$id'"; $result = mysql_query($sql); // 只要放一个正确的sql就可以执行 while (list($id, $hits, $title, $date, $contents) = mysql_fetch_row($result)) { // 遍历表内容 echo "id = {$id}<br>"; echo "hits = {$hits}<br>"; echo "title = {$title}<br>"; ?>titleLink: <a href="view.php?id=<?php echo $id; ?>"><?php echo $title."<br>"; ?></a><?php echo "date = {$date}<br>"; echo "contents = {$contents}<br>"; echo "contents = ".iconv_substr($contents,0,15)."...<br>"; // iconv_substr — 截取字符串的部分 ?>edit: <a href="edit.php?id=<?php echo $id; ?>">edit</a><?php echo "<br>"; ?>delete: <a href="del.php?id=<?php echo $id; ?>">delete</a><?php echo "<br>"; }
del.php:
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入连接数据库 //获取数据库表数据 if (!empty($_GET['id'])) { $edit = $_GET['id']; $sql = "select * from simpleblog where id='$edit'"; $query = mysql_query($sql); $rs = mysql_fetch_array($query); } //更新数据库表数据 if (!empty($_POST['sub'])) { $title = $_POST['title']; //获取title表单内容 $con = $_POST['con']; //获取contents表单内容 $hid = $_POST['hid']; $sql= "update simpleblog set title='$title', contents='$con' where id='$hid' "; mysql_query($sql); echo "<script>alert('update success.');location.href='index.php'</script>"; } ?> <form action="edit.php" method="post"> <input type="hidden" name="hid" value="<?php echo $rs['id'];?>"> title :<br> <input type="text" name="title" value="<?php echo $rs['title'];?>"> <br><br> contents:<br> <textarea rows="5" cols="50" name="con" ><?php echo $rs['contents'];?></textarea><br><br> <input type="submit" name="sub" value="submit"> </form>
위 내용은 안드로이드 프로그래머를 위한 PHP 개발 학습(28)-간단한 블로그 시스템-PhpStorm의 내용입니다. 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 주목해주세요!