Add function (2...LOGIN

Add function (2)

Let’s take a look at the php code to add functions.

Adding data must be added to the database, so the first step must be to connect to the database first.

<?php
$link = mysqli_connect("localhost","root","root","joke");
  if (!$link) {
             die("连接失败: " . mysqli_connect_error());
        }
?>

We may also need to use the code to connect to the database later, so we can Extract this code and put it in a separate file. In the future, you can directly reference the file, such as naming it config.php. In the future, we only need to include and reference it.

The next step is to receive the value passed by the front-end form page. It is the same as the previous registration.

The other thing about image uploading is to implement it by instantiating the calling class method. Of course you can also write it yourself.

<?php
if($_POST){
  date_default_timezone_set("PRC");         //设置时区
  $author = isset($_POST['author'])?$_POST['author']:"";     //获取表单传递过来的值
  $content = isset($_POST['content'])?$_POST['content']:"";
  $cid = isset($_POST['cid'])?$_POST['cid']:"";
 require 'fileupload.class.php';                           //引用类文件
 $upobj=new FileUpload();                                  //实例化调用类
 $ret=$upobj->upload('pic');
 if($ret==1){
   $creat_time = date("Y-m-d H:i:s");
?>

The function date_default_timezone_set() sets the time zone, and "PRC" represents the Chinese time zone.

When using the reference class method, you need to know something about the class file, otherwise reference errors may occur.

The next step is to insert the received data into the database, use the insert into statement

<?php
if($author && $content && $creat_time && $cid){
   $sql ="insert into list(author,content,creat_time,image,cid) values('$author','$content','$creat_time','{$upobj->newpath}',$cid)";
   $rel = mysqli_query($link,$sql);
   if($rel) {
     echo "添加成功" . "<br/><br/>";
     echo "<a href='list.php'>跳转至列表页面</a>";
   }
 }else {
   echo "添加失败" . "<br/><br/>";
   echo "<a href='add.php'>跳转至添加页面</a>";
 }
}
?>

First write the insert statement, then execute the insert statement, assign the return value to the variable $rel, and determine whether $rel is If there is no value, the addition will be successful. If not, the addition will fail.

Finally, our added function is complete.

Next Section
<?php session_start(); header("content-type:text/html;charset=utf-8"); //连接数据库 $link = mysqli_connect("localhost","root","root","joke"); if (!$link) { die("连接失败: " . mysqli_connect_error()); } if($_POST){ date_default_timezone_set("PRC"); $author = isset($_POST['author'])?$_POST['author']:""; $content = isset($_POST['content'])?$_POST['content']:""; $cid = isset($_POST['cid'])?$_POST['cid']:""; require 'fileupload.class.php'; $upobj=new FileUpload(); $ret=$upobj->upload('pic'); if($ret==1){ $creat_time = date("Y-m-d H:i:s"); if($author && $content && $creat_time && $cid){ $sql ="insert into list(author,content,creat_time,image,cid) values('$author','$content','$creat_time','{$upobj->newpath}',$cid)"; $rel = mysqli_query($link,$sql); if($rel) { echo "添加成功" . "<br/><br/>"; echo "<a href='list.php'>跳转至列表页面</a>"; } }else { echo "添加失败" . "<br/><br/>"; echo "<a href='add.php'>跳转至添加页面</a>"; } } ?> <!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"> <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="" 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"> <select name="cid" class="input w50"> <option value="">请选择分类</option> <option value="1">搞笑段子</option> <option value="2">搞笑图片</option> </select> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label>内容:</label> </div> <div class="field"> <textarea name="content" class="" id="content" style="height:450px;"></textarea> <div class="tips"></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="creat_time" onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" value="" 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"> <input type="text" class="input w50" name="author" value="" /> <div class="tips"></div> </div> </div> <div class="clear"></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> <script type="text/javascript"> //实例化编辑器 //建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例 UE.getEditor('content',{initialFrameWidth:1500,initialFrameHeight:400,}); </script> </body> </html>
submitReset Code
ChapterCourseware