Android-Programmierer lernen PHP-Entwicklung (28) – Einfaches Blog-System – PHPStorm

黄舟
Freigeben: 2023-03-06 10:32:02
Original
987 Leute haben es durchsucht


Einfaches Blogsystem, Veröffentlichung, Bearbeitung, Löschung, Datenbankübungen ~ Ignorieren Sie die Sicherheit vorerst, schauen wir uns zuerst die GIF-Animation an:



Sehen Sie sich den Datenbank-Screenshot an:



Okay, schauen Sie sich den Code an:

conn.php:

<?php
    /**
     * mysql_query — 发送一条 MySQL 查询
     */

    /**
     * 连接数据库(返回资源)
     */
    @mysql_connect("127.0.0.1:3306","root","") or die("mysql数据库连接失败");

    /**
     * 选择一个数据库作为默认的数据库使用
     */
    @mysql_select_db("blog")or die("db连接失败");
Nach dem Login kopieren


Index .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=&#39;align:"right"&#39;>
    <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>";
    }
Nach dem Login kopieren


add.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[&#39;sub&#39;])) {
        $title = $_POST[&#39;title&#39;];  //获取title表单内容
        $con = $_POST[&#39;con&#39;];      //获取contents表单内容
        $sql= "insert into simpleblog values(null,&#39;0&#39;,&#39;$title&#39;,now(),&#39;$con&#39;)";
        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>
Nach dem Login kopieren

view.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[&#39;id&#39;];

    $sql = "select id,hits,title,date,contents from simpleblog where id=&#39;$id&#39;";

    $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>";
    }
Nach dem Login kopieren

edit.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[&#39;id&#39;])) {
        $edit = $_GET[&#39;id&#39;];
        $sql = "select * from simpleblog where id=&#39;$edit&#39;";
        $query = mysql_query($sql);
        $rs = mysql_fetch_array($query);
    }

    //更新数据库表数据
    if (!empty($_POST[&#39;sub&#39;])) {
        $title = $_POST[&#39;title&#39;];  //获取title表单内容
        $con = $_POST[&#39;con&#39;];      //获取contents表单内容
        $hid = $_POST[&#39;hid&#39;];
        $sql= "update simpleblog set title=&#39;$title&#39;, contents=&#39;$con&#39; where id=&#39;$hid&#39; ";
        mysql_query($sql);
        echo "<script>alert(&#39;update success.&#39;);location.href=&#39;index.php&#39;</script>";

    }
?>

<form action="edit.php" method="post">
    <input type="hidden" name="hid" value="<?php echo $rs[&#39;id&#39;];?>">
    title   :<br>
    <input type="text" name="title" value="<?php echo $rs[&#39;title&#39;];?>">
    <br><br>
    contents:<br>
    <textarea rows="5" cols="50" name="con" ><?php echo $rs[&#39;contents&#39;];?></textarea><br><br>
    <input type="submit"  name="sub" value="submit">
</form>
Nach dem Login kopieren

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[&#39;id&#39;])) {
        $del = $_GET[&#39;id&#39;];  //删除blog
        $sql= "delete from simpleblog where id=&#39;$del&#39; ";
        mysql_query($sql);
        echo "delete success!";
    }
?>
Nach dem Login kopieren


Das Obige ist der Inhalt von Android-Programmierern, die PHP-Entwicklung lernen (28) - Einfaches Blogsystem - PhpStorm. Weitere verwandte Inhalte finden Sie unter PHP chinesische Website (www.php .cn)!




Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!