Home > Article > Backend Development > Teach you step by step how to develop a news release website with PHP
With the popularity and rapid development of the Internet, news release websites have become an important channel for people to obtain news information. As a powerful server-side programming language, PHP is widely used in website development. This article will teach you step by step how to develop a news release website using PHP.
<?php $db_host = 'localhost'; $db_username = 'root'; $db_password = 'password'; $db_name = 'news'; $conn = new mysqli($db_host, $db_username, $db_password, $db_name); if ($conn->connect_error) { die("数据库连接失败:" . $conn->connect_error); }
<?php include 'db_connect.php'; $category = $_GET['category']; $sql = "SELECT * FROM news_articles WHERE category='$category'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<h3>" . $row['title'] . "</h3>"; echo "<p>" . $row['content'] . "</p>"; } } else { echo "暂无新闻"; } $conn->close();
<?php include 'db_connect.php'; $id = $_GET['id']; $sql = "SELECT * FROM news_articles WHERE id='$id'"; $result = $conn->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); echo "<h1>" . $row['title'] . "</h1>"; echo "<p>" . $row['content'] . "</p>"; } else { echo "新闻不存在"; } $conn->close();
<?php include 'db_connect.php'; $sql = "SELECT * FROM news_articles ORDER BY timestamp DESC"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<h3>" . $row['title'] . "</h3>"; echo "<p>" . $row['content'] . "</p>"; } } else { echo "暂无新闻"; } $conn->close();
<?php include 'db_connect.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $category = $_POST['category']; $title = $_POST['title']; $content = $_POST['content']; $timestamp = date('Y-m-d H:i:s'); $sql = "INSERT INTO news_articles (category, title, content, timestamp) VALUES ('$category', '$title', '$content', '$timestamp')"; if ($conn->query($sql) === TRUE) { echo "发布成功"; } else { echo "发布失败:" . $conn->error; } } $conn->close(); ?> <form method="POST" action="admin.php"> <input type="text" name="category" placeholder="分类" required> <input type="text" name="title" placeholder="标题" required> <textarea name="content" placeholder="内容" required></textarea> <button type="submit">发布新闻</button> </form>
At this point, we have completed the development of a simple news release website. You can further optimize and expand according to actual needs, such as adding user authentication, news editing, comments and other functions. I hope this article can help you quickly get started developing a news release website in PHP.
The above is the detailed content of Teach you step by step how to develop a news release website with PHP. For more information, please follow other related articles on the PHP Chinese website!