php原生开发新闻站之修改新闻LOGIN

php原生开发新闻站之修改新闻

这节课继续给大家介绍新闻的增删改查的修改,上节课我们完成添加新闻的功能,其实修改新闻跟添加新闻道理是一样的,只是修改我们在input框要获取数据的值,然后修改,保存到数据库,下面我们就看下如何实现!

首先创建一个php文件,我们叫new_edit.php 

我们在新闻列表展示页面,找到修改新闻的按钮,然后给他一个连接,通过id 传输信息,我们在new_edit.php通过id来接受

<a class='button border-main' href='new_edit.php?id=<?php echo $val['id'];?>'>
<span class='icon-edit'></span> 修改</a>

然后我们连接数据库

<?php
// 连接mysql数据库
$link = mysqli_connect('localhost', 'root', 'root');
if (!$link) {
    echo "connect mysql error!";
    exit();
}
// 选中数据库 news为数据库的名字
$db_selected = mysqli_select_db($link, 'news');
if (!$db_selected) {
    echo "<br>selected db error!";
    exit();
}
// 设置mysql字符集 为 utf8
$link->query("set names utf8");

然后设置图片,获取图片

<?php
if(count($_POST)>0){

if( count($_FILES['pic']) > 0 && $_FILES['pic']['name']   ) { // 保存头像图片
    $flag = true;
}
        // 检查文件类型
        if(  !in_array($_FILES['pic']['type'], array('image/jpeg','image/png', 'image/gif')) ){
            echo "只运行上传jpg或png图片, 文件类型不合法,不允许上传";
        }
        // 检查文件大小
        if ($_FILES['pic']['size'] > 5*1024*1024){
            echo "文件最大尺寸为5M,不允许上传.";
        }
    if ( $flag ){
        // 获取文件后缀名
        $file_ext= pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION);
        $tmp_file = $_FILES['pic']['tmp_name']; // 临时文件
        $dest_file = pathinfo($tmp_file, PATHINFO_FILENAME).".".$file_ext; // 保存的文件名
        //move_uploaded_file($tmp_file, "d:/wamp/www/upload/".$dest_file);  // 使用绝对地址保存图片
        move_uploaded_file($tmp_file, "../../upload/".$dest_file); // 使用相对地址保存图片
        $avatar_path ="../../upload/".$dest_file; // 注意,保存的时候,设置从服务器的根目录开始
    }
    if( !$avatar_path ){
    $avatar_path = $arr_recommend['img'];
}

然后通过id获取信息,查询数据表,执行SQL语句

// 根据id 获取用户信息
$id = $_GET['id'];
if( !is_numeric($id) ) {
    echo "ERROR!";
    exit;
}
//获取查询信息
$sql ="select * from new where id = $id";
$result = mysqli_query($link,$sql);
$arr_news = mysqli_fetch_array($result, MYSQL_ASSOC);

然后使用update set将数据保存数据库,代码如下:

 $update_sql = "update new set category_id = '{$_POST['category_id']}',
                                        title ='{$_POST['title']}',
                                       content ='{$_POST['content']}', 
                                       tag ='{$_POST['tag']}', 
                                       author ='{$_POST['author']}', 
                                       pic ='{$avatar_path}', 
                                       created_at ='{$_POST['created_at']}'
                                       where id ='{$_POST['id']}' 
                                       ";
    $result = mysqli_query($link,$update_sql);
    if($result){
        echo "添加成功!";
        $url = "http://127.0.0.1/news/Admin/new/new_list.php";
        header("Location: $url");
        exit;
    }else{
        echo "修改失败!";
    }
}

接着就是给每个input框中的value值赋值:

<form method="post" class="form-x" action="" enctype="multipart/form-data">
    <input type="hidden" name="id" value="<?php echo $arr_news['id'];?>">
    <div class="form-group">
        <div class="label">
            <label>分类ID:</label>
        </div>
        <div class="field">
            <select name="category_id" required class="form-select">
                <option value="">-请选择-</option>
                <?php
                foreach( $arr_news_category as $val){
                    $str_selected = "";
                    if( $arr_news['category_id'] == $val['id']){
                        $str_selected = "selected";
                    }
                    echo "<option value='{$val['id']}' $str_selected>{$val['name']}</option>";
                }
                ?>
            </select>

            <div class="tips"></div>
        </div>
    </div>
    <div class="form-group">
        <div class="label">
            <label>标题:</label>
        </div>
        <div class="field">
            <input type="text" class="input w50" value="<?php echo $arr_news['title'];?>" name="title" data-validate="required:请输入标题" />
            <div class="tips"></div>
        </div>
    </div>


    <div class="clear"></div>
    <div class="form-group">
        <div class="label">
            <label>关键字:</label>
        </div>
        <div class="field">
            <input type="text" class="input" name="tag" value="<?php echo $arr_news['tag'];?>" />
        </div>
    </div>

    <div class="form-group">
        <div class="label">
            <label>内容:</label>
        </div>
        <div class="field">
            <textarea name="content" class="input" id="content" style="height:450px; width: 98%; border:1px solid #ddd;"><?php echo $arr_news['content'];?></textarea>

        </div>
    </div>

    <div class="form-group">
        <div class="label">
            <label>作者:</label>
        </div>
        <div class="field">
            <input type="text" class="input w50" name="author" value="<?php echo $arr_news['author'];?>"  />
            <div class="tips"></div>
        </div>
    </div>

    <div class="form-group">
        <div class="label">
            <label>图片:</label>
        </div>
        <div class="field">
            <input type="file" id="url1" name="pic" class="input tips" style="width:25%; float:left;" value="<?php echo $arr_news['pic'];?>"  data-toggle="hover" data-place="right" data-image="" />
            <input type="button" class="button bg-blue margin-left" id="image1" value="+ 浏览上传"  style="float:left;">
            <div class="tipss">图片尺寸:500*500</div>
        </div>
    </div>


    <div class="form-group">
        <div class="label">
            <label>发布时间:</label>
        </div>
        <div class="field">
            <script src="../js/laydate/laydate.js"></script>
            <input type="text" class="laydate-icon input w50" name="created_at" onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" value="<?php echo $arr_news['created_at']?>"  data-validate="required:日期不能为空" style="padding:10px!important; height:auto!important;border:1px solid #ddd!important;" />
            <div class="tips"></div>
        </div>
    </div>

    <div class="form-group">
        <div class="label">
            <label></label>
        </div>
        <div class="field">
            <button class="button bg-main icon-check-square-o" type="submit"> 提交</button>
        </div>
    </div>
</form>

同样有个分类要处理,我们要先查询分类表数据:

//获取所有的新闻分类
$sql  = "select * from new_category ";
$result = mysqli_query($link, $sql);
$arr_news_category = mysqli_fetch_all($result, MYSQL_ASSOC);

在分类名的input框中遍历出分类信息:

<div class="form-group">
    <div class="label">
        <label>分类ID:</label>
    </div>
    <div class="field">
        <select name="category_id" required class="form-select">
            <option value="">-请选择-</option>
            <?php
            foreach( $arr_news_category as $val){
                $str_selected = "";
                if( $arr_news['category_id'] == $val['id']){
                    $str_selected = "selected";
                }
                echo "<option value='{$val['id']}' $str_selected>{$val['name']}</option>";
            }
            ?>
        </select>

OK!这里就完成了新闻的修改!

1743.png

下一节
<?php include_once "../common/mysql.php"; if(count($_POST)>0){ if( count($_FILES['pic']) > 0 && $_FILES['pic']['name'] ) { // 保存头像图片 $flag = true; } // 检查文件类型 if( !in_array($_FILES['pic']['type'], array('image/jpeg','image/png', 'image/gif')) ){ echo "只运行上传jpg或png图片, 文件类型不合法,不允许上传"; } // 检查文件大小 if ($_FILES['pic']['size'] > 5*1024*1024){ echo "文件最大尺寸为5M,不允许上传."; } if ( $flag ){ // 获取文件后缀名 $file_ext= pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION); $tmp_file = $_FILES['pic']['tmp_name']; // 临时文件 $dest_file = pathinfo($tmp_file, PATHINFO_FILENAME).".".$file_ext; // 保存的文件名 //move_uploaded_file($tmp_file, "d:/wamp/www/upload/".$dest_file); // 使用绝对地址保存图片 move_uploaded_file($tmp_file, "../../upload/".$dest_file); // 使用相对地址保存图片 $avatar_path ="../../upload/".$dest_file; // 注意,保存的时候,设置从服务器的根目录开始 } if( !$avatar_path ){ $avatar_path = $arr_recommend['img']; } $update_sql = "update new set category_id = '{$_POST['category_id']}', title ='{$_POST['title']}', content ='{$_POST['content']}', tag ='{$_POST['tag']}', author ='{$_POST['author']}', pic ='{$avatar_path}', created_at ='{$_POST['created_at']}' where id ='{$_POST['id']}' "; $result = mysqli_query($link,$update_sql); if($result){ echo "添加成功!"; $url = "http://127.0.0.1/news/Admin/new/new_list.php"; header("Location: $url"); exit; }else{ echo "修改失败!"; } } //获取所有的新闻分类 $sql = "select * from new_category "; $result = mysqli_query($link, $sql); $arr_news_category = mysqli_fetch_all($result, MYSQL_ASSOC); // 根据id 获取用户信息 $id = $_GET['id']; if( !is_numeric($id) ) { echo "ERROR!"; exit; } //获取查询信息 $sql ="select * from new where id = $id"; $result = mysqli_query($link,$sql); $arr_news = mysqli_fetch_array($result, MYSQL_ASSOC); ?> <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <meta name="renderer" content="webkit"> <title></title> <link rel="stylesheet" href="../css/pintuer.css"> <link rel="stylesheet" href="../css/admin.css"> <script src="../js/jquery.js"></script> <script src="../js/pintuer.js"></script> <script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.config.js"></script> <script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.all.min.js"> </script> <!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败--> <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文--> <script type="text/javascript" charset="utf-8" src="../ueditor/lang/zh-cn/zh-cn.js"></script> </head> <body> <div class="panel admin-panel"> <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>修改内容</strong></div> <div class="body-content"> <form method="post" class="form-x" action="" enctype="multipart/form-data"> <input type="hidden" name="id" value="<?php echo $arr_news['id'];?>"> <div class="form-group"> <div class="label"> <label>分类ID:</label> </div> <div class="field"> <select name="category_id" required class="form-select"> <option value="">-请选择-</option> <?php foreach( $arr_news_category as $val){ $str_selected = ""; if( $arr_news['category_id'] == $val['id']){ $str_selected = "selected"; } echo "<option value='{$val['id']}' $str_selected>{$val['name']}</option>"; } ?> </select> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label>标题:</label> </div> <div class="field"> <input type="text" class="input w50" value="<?php echo $arr_news['title'];?>" name="title" data-validate="required:请输入标题" /> <div class="tips"></div> </div> </div> <div class="clear"></div> <div class="form-group"> <div class="label"> <label>关键字:</label> </div> <div class="field"> <input type="text" class="input" name="tag" value="<?php echo $arr_news['tag'];?>" /> </div> </div> <div class="form-group"> <div class="label"> <label>内容:</label> </div> <div class="field"> <textarea name="content" class="input" id="content" style="height:450px; width: 98%; border:1px solid #ddd;"><?php echo $arr_news['content'];?></textarea> </div> </div> <div class="form-group"> <div class="label"> <label>作者:</label> </div> <div class="field"> <input type="text" class="input w50" name="author" value="<?php echo $arr_news['author'];?>" /> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label>图片:</label> </div> <div class="field"> <input type="file" id="url1" name="pic" class="input tips" style="width:25%; float:left;" value="<?php echo $arr_news['pic'];?>" data-toggle="hover" data-place="right" data-image="" /> <input type="button" class="button bg-blue margin-left" id="image1" value="+ 浏览上传" style="float:left;"> <div class="tipss">图片尺寸:500*500</div> </div> </div> <div class="form-group"> <div class="label"> <label>发布时间:</label> </div> <div class="field"> <script src="../js/laydate/laydate.js"></script> <input type="text" class="laydate-icon input w50" name="created_at" onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" value="<?php echo $arr_news['created_at']?>" data-validate="required:日期不能为空" style="padding:10px!important; height:auto!important;border:1px solid #ddd!important;" /> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label></label> </div> <div class="field"> <button class="button bg-main icon-check-square-o" type="submit"> 提交</button> </div> </div> </form> </div> </div> </body> </html> <script type="text/javascript"> //实例化编辑器 //建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例 UE.getEditor('content',{initialFrameWidth:1500,initialFrameHeight:400,}); </script>
提交重置代码
章节课件