会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132553个问题
JAVA 全系列/第三十阶段:面试和成功求职的秘技/面试前的准备 28112楼
Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/Softmax回归 28113楼
WEB前端全系列/第十六阶段:React企业级项目/宜居(商城类)高级React实战项目(旧) 28115楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 28119楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 28120楼

index.php

<?php
//获取数据库中的数据
try{
    $con=new PDO("mysql:host=localhost;dbname=beixidb;charset=utf8;port=3306",'root','');
    $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $res=$con->query('select * from news');
    $data=$res->fetchAll(PDO::FETCH_ASSOC);
    // print_r($data);
    //字符串的拼接
    $str='';
    for($i=0;$i<count($data);$i++){
        $str.="<tr><td>{$data[$i]['id']}</td><td>{$data[$i]['title']}</td><td>{$data[$i]['author']}</td><td>{$data[$i]['des']}</td>
        <td>{$data[$i]['content']}</td><td>{$data[$i]['time']}</td><td><a href='#'>删除</a></br><a href='#'>修改</a></td></tr>";
    }
}catch(PDOException $e){
    echo $e->getMessage();
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container{
            width:800px;
            height:600px;
            margin:20px auto;
            border:1px solid #ccc;
            overflow:hidden;
        }
        .left,.right{
            float:left;
        }
        .left{
            width:120px;
        }
        table{
            width:620px;
        }
        .left a{
            display:block;
            text-decoration:none;
            color:#000;
            padding:10px 0;
            border-bottom:1px solid #efefef;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="left">
            <a href="">所有文档列表</a>
            <a href="添加文档.html">添加列表</a>
        </div>
        <div class="right">
        <table border="1" cellpadding="0" cellspacing="0" width="600">
        <tr>
            <th>ID</th>
            <th>标题</th>
            <th>作者</th>
            <th>简介</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
        <?php echo $str?>
    </table>
        </div>
    </div>
</body>
</html>

添加文档.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container{
            width:800px;
            margin:20px auto;
            border:1px solid #ccc;
            overflow:hidden;
        }
        .left,.right{
            float:left;
        }
        .left{
            width:120px;
        }
        input{
            width: 500px;
        }
        .left a{
            display:block;
            text-decoration:none;
            color:#000;
            padding:10px 0;
            border-bottom:1px solid #efefef;
        }
        textarea{
            vertical-align: top;
            margin-top: 5px;
            width: 500px;
            resize: none;
        }
        .btn{
            display: block;
            width: 80px;
            height: 50px;
            border: none;
            background: orange;
            border-radius: 5px;
            color: #fff;
            margin: 10px auto;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="left">
            <a href="#">添加文档页面</a>
        </div>
        <div class="right">
            <form action="addnews.php" method="get">
                标题:<input type="text" name="title"><br/>
                作者:<input type="text" name="author"><br/>
                简介:<input type="text" name="des"><br/>
                内容:<textarea type="text" name="content" cols="30" rows="10"></textarea><br/>
                <input type="submit" value="保存" class="btn">
            </form>
        </div>
    </div>
</body>
</html>

addnews.php

<?php
    echo '<pre>';
//接收用户传来的数据
    $title=isset($_GET['.title'])?$_GET['title']:'';
    $author=isset($_GET['.author'])?$_GET['author']:'';
    $des=isset($_GET['.des'])?$_GET['des']:'';
    $content=isset($_GET['.content'])?$_GET['content']:'';
    //生成一个时间戳
    $time=time();
    //写入数据库


    $con=new PDO("mysql:host=localhost;dbname=beixidb;charset=utf8;port=3306",'root','');
    $res=$con->query("insert into news (title,author,des,content,time) values('{$title}','$author','$des','$content','$time')");
    if($res){
        echo '<script> alert("添加成功");window.location.href="index.php"</script>';
    }
    else{
        echo '添加失败';
    }
?>

老师,为什么这个案例添加成功后 标题 作者等信息不显示,只显示时间戳,是没添加进去吗?具体代码如上:

效果图附下:

图片.png

WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 28121楼
Python 全系列/第一阶段:Python入门/函数和内存分析 28123楼
Python 全系列/第一阶段:Python入门/函数和内存分析 28124楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 28125楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637