我的PHP笔记
1、用过的函数
1、empty()
empty()
--------
if (empty($title)) {
...........
}
该函数用于检查一个变量是否为空。
PHP的一些方法
1、连接数据库先
// 三个值 第一个是 要连接的服务器,一般都是localhost:3306,就是在你网站那边本地的服务器
// 第二个是username 就是用户名
// 第三个是password 密码,不用多说
$conn = new mysqli($servername, $username, $password);
我的Thinkphp笔记
1、前端提交数据给数据库
<form id="changepwd-form" class="form-horizontal" method="POST" action="">
<input type="text" name="isname" placeholder="123456">
<input type="submit">
</form>
// 判断是否是post请求
if ($this->request->isPost()) {
// 接收前端发送的数据
$isname = $this->request->post("isname");
// 模型实例化
$user = model('Qiantai');
// 将数据插入数据库
$user->data([
'name' => $isname
]);
$user->save();
}
2、使用接口来提交数据库
<!--前端提交-->
<form action="http://xxxxxx/api/index/tijiao">
<input type="text" name="title">
<input type="text" name="content">
<input type="submit" vuale="123456">
</form>
public function tijiao($title,$content)
{
// 模型实例化
$a = new Question();
// 创建数组并添加进数据库
$data = ([
'title' => $title,
'content' => $content
]);
$a->save($data);
}
3、如何判断上传的数据是否重复
public function index()
{
$a = new Feed();
if ($this->request->isPost()) {
$title = $this->request->post("title");
$content = $this->request->post("content");
// 我要的是对title进行判断是否重复,首先先获取数据库中的title 之后进行判断
$titlea = Feed::where('title','=',$title)->select();
// 如果未重复,查询的结果将会为空
// 之后再进行存储
if ($titlea == NULL) {
$data = ([
'title' => $title,
'content' => $content
]);
$a->save($data);
} else {
echo '该数组有重复值';
}
}
return $this->view->fetch();
}
登录系统
前端
<form action="" method="post">
<input type="text" name="name">
<input type="text" name="mm">
<input type="submit">
</form>
PHP
// 登录页面
public function login()
{
if (empty($_COOKIE['name'])) {
if ($this->request->isPost()) {
$login = input('post.');
if (empty($login['name'])) {
echo '账号密码不能为空';
} else {
$yz = Dl::where('name','=',$login['name'])->select();
if ($yz != null) {
$user = Dl::get([
'name' => $login['name'],
'password' => $login['mm']
]);
if ($user == null) {
echo '密码错误';
} else {
echo '密码正确';
cookie('name', $login['name'],10000);
$this->redirect(url('index/qwq'));
}
} else {
echo '用户名不存在';
}
}
}
} else {
$this->redirect(url('index/qwq'));
}
return $this->view->fetch();
}
// 用户页面
public function Users()
{
if (empty($_COOKIE['name'])) {
echo '欢迎普通用户';
} else {
echo "您好: " . cookie('name') . ', <a href="' . url('index/loginout') . '">退出</a>';
}
return $this->view->fetch();
}
// 退出登录方法
public function loginout()
{
cookie('name', null);
$this->redirect(url('index/abc'));
return $this->view->fetch();
}
PHP验证器
方法一,直接使用命令在\app\validate\User,创建一个验证器类
php think make:validate User
namespace app\validate;
use think\Validate;
class User extends Validate
{
// 验证规则
protected $rule = [
'name' => 'require',
'age' => 'require|min:18'
];
// 错误信息
protected $message = [
'name.require' => '用户名不能为空',
'age.require' => '年龄不能为空',
'age.min' => '年龄不能小于18'
];
}
调用
<?php
namespace app\controller;
use app\validate\User as UserValidate;
use think\exception\ValidateException;
class User
{
public function index()
{
try {
validate(UserValidate::class)->batch(true)->check([
'name' => '小明',
'ega' => '18'
]);
} catch (ValidateException $e) {
return dump($e->getError());
}
}
}
TP Where运算符
TP运算符 | SQL运算符 | 例子 | 实际查询条件 |
---|---|---|---|
eq | = | where(‘id’,‘eq’,‘100’) | $map[‘id’] = 100; |
neq | != | where(‘id’,‘neq’,‘100’) | id != 100 |
gt | > | where(‘id’,‘gt’,‘100’) | id > 100 |
egt | >= | where(‘id’,‘eqt’,‘100’) | id >= 100 |
lt | < | where(‘id’,‘it’,‘100’) | id < 100 |
elt | <= | where(‘id’,‘elt’,‘100’) | id <= 100 |
like | like | where(‘username’,‘lick’,’%name%’) | username like ‘%user%’ |
between | between and | where(‘id’,‘between’,‘1,100’) | id BETWEEN 1 AND 100 |
not between | not between and | where(‘id’,‘not between’,‘1,100’) | id NOT BETWEEN 1 AND 100 |
in | in | where(‘id’,‘in’,‘10,11’) | id in(10,11) |
not in | not in | where(‘id’,‘not in’,‘10,11’) | id not in(10,11) |