静态网页制作——在phpsrorm中用实现前台添加留言内容插入数据库

1.http://localhost/phpMyAdmin/——>查找owz数据库——>message表:
2.C:\phpStudy\PHPTutorial\WWW\owz\application\index\controller\Message.php
修改内容:
<?php
namespace app\index\controller;
use think\Controller;
class Message extends Controller
{
    public function index()
    {
        if (request()->isPost()) { //提交Post传过来的数据
            $data = [
                'name' => input('name'),
                'telephone' => input('telephone'),
                'address' => input('address'),
                'email' => input('email'),
                'content' => input('content'),
            ];
            $validate = \think\Loader::validate('Message');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
                die;
            }
            if (db('admin')->insert($data)) {
                return $this->success('添加管理员成功!', 'lst');
            } else {
                return $this->error('添加管理员失败!');
            }
            return;
        }
//        return $this->fetch();
        return $this->fetch('message');
   }
 }
3.C:\phpStudy\PHPTutorial\WWW\owz\application\index——>创建validate文件夹
——>Message.php文件;
只验证:姓名、联系电话、留言内容
内容:
<?php
namespace app\admin\validate;
use think\Validate;
class Admin extends Validate
{
 protected $rule = [
     'name' => 'require',
     'telephone' => 'require',
     'content' => 'require',
 ];
 protected $message = [
     'name.require'=>'姓名不能为空',
     'telephone.require'=>'联系电话不能为空',
     'content.require'=>'留言内容!',
 ];

4.C:\phpStudy\PHPTutorial\WWW\owz\application\index\controller\Message.php
修改内容:
<?php
namespace app\index\controller;
use think\Controller;
class Message extends Controller
{
    public function index()
    {
        if (request()->isPost()) { //提交Post传过来的数据
            $data = [
                'name' => input('name'),
                'telephone' => input('telephone'),
                'address' => input('address'),
                'email' => input('email'),
                'content' => input('content'),
            ];
            $validate = \think\Loader::validate('Message');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
                die;
            }
            if (db('message')->insert($data)) {
                return $this->success('留言成功!', 'lst');
            } else {
                return $this->error('留言失败!');
            }
            return;
        }
//        return $this->fetch();
        return $this->fetch('message');
    }
}
5.C:\phpStudy\PHPTutorial\WWW\owz\application\index\view\Message\message.html
在留言内容下面添加按钮:
<tr>
                        <td width="119" height="164" align="right" valign="top">留言内容:</td>
                        <td width="431" height="164" align="left" valign="top" class="tab2"><textarea name="content" id="content"></textarea></td>
                    </tr>
                    <tr>
                        <td width="119" height="40" align="right" valign="top">&nbsp;</td>
                        <td width="431" height="40" align="left" valign="top"><a href="#"><img src="/owz/public/static/index/images/tjbt.jpg" width="81" height="28" /></a></td>
                    </tr>
                    <tr>
                        <th></th>
                        <td>
                            <input class="btn btn-primary btn6 mr10" value="提交" type="submit" >
                            <input class="btn btn6" onclick="history.go(-1)"  value="返回" type="button">
                        <td>
                        </td>

添加提交和返回按钮成功!
http://www.iheyu.com/owz/public/index.php/index/message
6.提交按钮没有反应
C:\phpStudy\PHPTutorial\WWW\owz\application\index\view\Message\message.html
在div 和table中间加from
添加内容:
<form action="" method="post" id="myfrom" name="myfrom" enctype="multipart/form-data">
                    <table width="550" border="0" cellspacing="0" cellpadding="0" class="tab">
                        <tr>
                            <td width="119" height="40" align="right" valign="top">姓  名:</td>
                            <td width="431" height="40" align="left" valign="top" class="tab1"><input type="text" name="name" id="name" /></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">联系电话:</td>
                            <td width="431" height="40" align="left" valign="top" class="tab1"><input type="text" name="telephone" id="telephone" /></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">联系地址:</td>
                            <td width="431" height="40" align="left" valign="top" class="tab1"><input type="text" name="address" id="address" /></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">电子邮箱:</td>
                            <td width="431" height="40" align="left" valign="top" class="tab1"><input type="text" name="email" id="email" /></td>
                        </tr>
                        <tr>
                            <td width="119" height="164" align="right" valign="top">留言内容:</td>
                            <td width="431" height="164" align="left" valign="top" class="tab2"><textarea name="content" id="content"></textarea></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">&nbsp;</td>
                            <td width="431" height="40" align="left" valign="top"><a href="#"><img src="/owz/public/static/index/images/tjbt.jpg" width="81" height="28" /></a></td>
                        </tr>
                        <tr>
                            <th></th>
                            <td>
                                <input class="btn btn-primary btn6 mr10" value="提交" type="submit" >
                                <input class="btn btn6" onclick="history.go(-1)"  value="返回" type="button">
                            <td>
                            </td>
                    </table>
                </form>
http://www.iheyu.com/owz/public/index.php/index/message
****************************************************************************************************************************
类不存在:app\common\validate\Message(bug)
C:\phpStudy\PHPTutorial\WWW\owz\application\index\validate\Message.php
<?php
namespace app\index\validate;
use think\Validate;
class Message extends Validate
{
 protected $rule = [
     'name' => 'require',
     'telephone' => 'require',
     'content' => 'require',
 ];
 protected $message = [
     'name.require'=>'姓名不能为空',
     'username.require'=>'联系电话不能为空',
     'content.require'=>'留言内容!',
 ];

}

*********************************************************************
验证成功!
http://www.iheyu.com/owz/public/index.php/index/message
***********************************************************************
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)(bug)
没有连接数据库
C:\phpStudy\PHPTutorial\WWW\owz\application\database.php

// 数据库名
    'database'        => 'owz',
*****************************************************************************
查看数据库message表中有没有数据,添加成功!
http://www.iheyu.com/owz/public/index.php/index/message
1.C:\phpStudy\PHPTutorial\WWW\owz\application\index\view\Message\message.html
原内容:
<tr>
                            <td width="119" height="164" align="right" valign="top">留言内容:</td>
                            <td width="431" height="164" align="left" valign="top" class="tab2"><textarea name="content" id="content"></textarea></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">&nbsp;</td>
	            <td width="431" height="40" align="left" valign="top"><a href="#"><img src="/owz/public/static/index/images/tjbt.jpg" width="81" height="28" /></a></td>
                            <td>
               
                                    <img src="/owz/public/static/index/images/tjbt.jpg" width="81" height="28" />
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input class="btn btn-primary btn6 mr10" value="提交" type="submit" >
                                <input class="btn btn6" onclick="history.go(-1)"  value="返回" type="button">
                            </td>
                        </tr>
                    </table>
<form action="" method="post" id="myform" name="myform" enctype="multipart/form-data">
和<a href="javascript:myform.submit();">
"submit"是提交!
修改为:
<form action="" method="post" id="myform" name="myform" enctype="multipart/form-data">
                    <table width="550" border="0" cellspacing="0" cellpadding="0" class="tab">
                        <tr>
                            <td width="119" height="40" align="right" valign="top">姓  名:</td>
                            <td width="431" height="40" align="left" valign="top" class="tab1"><input type="text" name="name" id="name" /></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">联系电话:</td>
                            <td width="431" height="40" align="left" valign="top" class="tab1"><input type="text" name="telephone" id="telephone" /></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">联系地址:</td>
                            <td width="431" height="40" align="left" valign="top" class="tab1"><input type="text" name="address" id="address" /></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">电子邮箱:</td>
                            <td width="431" height="40" align="left" valign="top" class="tab1"><input type="text" name="email" id="email" /></td>
                        </tr>
                        <tr>
                            <td width="119" height="164" align="right" valign="top">留言内容:</td>
                            <td width="431" height="164" align="left" valign="top" class="tab2"><textarea name="content" id="content"></textarea></td>
                        </tr>
                        <tr>
                            <td width="119" height="40" align="right" valign="top">&nbsp;</td>
                            <td>
                                <a href="javascript:myform.submit();">
                                    <img src="/owz/public/static/index/images/tjbt.jpg" width="81" height="28" />
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input class="btn btn-primary btn6 mr10" value="提交" type="submit" >
                                <input class="btn btn6" onclick="history.go(-1)"  value="返回" type="button">
                            </td>
                        </tr>
                    </table>
http://www.iheyu.com/owz/public/index.php/index/message/index.html
提交留可以使用!
2.去掉里面的:
 <tr>
                            <td>
                                <input class="btn btn-primary btn6 mr10" value="提交" type="submit" >
                                <input class="btn btn6" onclick="history.go(-1)"  value="返回" type="button">
                            </td>
                        </tr>
http://www.iheyu.com/owz/public/index.php/index/message/index.html


3.http://localhost/phpMyAdmin
在数据中查看是否能插入一条数据!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值