Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

新闻表单php(新闻的表格)

作者:至尊巅峰   发布日期:2025-08-14   浏览:485

以下是一个简单的新闻表单的PHP代码示例:

<!DOCTYPE html>
<html>
<head>
    <title>新闻表单</title>
</head>
<body>
    <h2>添加新闻</h2>
    <form method="post" action="process_news.php">
        <label for="title">标题:</label>
        <input type="text" id="title" name="title" required><br><br>

        <label for="content">内容:</label>
        <textarea id="content" name="content" required></textarea><br><br>

        <label for="author">作者:</label>
        <input type="text" id="author" name="author" required><br><br>

        <input type="submit" value="提交">
    </form>
</body>
</html>

在上面的代码中,我们创建了一个简单的HTML表单来添加新闻。表单使用POST方法将数据提交到process_news.php文件进行处理。

process_news.php文件中,你可以使用以下代码来处理表单数据并将其插入到数据库中:

<?php
// 连接到数据库
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 获取表单数据
$title = $_POST['title'];
$content = $_POST['content'];
$author = $_POST['author'];

// 插入数据到数据库
$sql = "INSERT INTO news (title, content, author) VALUES ('$title', '$content', '$author')";

if ($conn->query($sql) === TRUE) {
    echo "新闻添加成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

// 关闭数据库连接
$conn->close();
?>

上面的代码将获取表单中的标题、内容和作者,并将它们插入到名为news的数据库表中的对应列中。

请注意,上述代码中的数据库连接信息($servername$username$password$dbname)需要根据你自己的数据库设置进行修改。

上一篇:excel上传 php(怎么传excel)

下一篇:php gd2.so

大家都在看

php session用法

php 定义常量

phpisset函数

php后端

php爬虫框架

php读取csv文件

php 三元表达式

php文件加密

php 判断是否为空

php 拆分字符串

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站