网页登录接口php,thinkphp-登入接口示例

新建数据表

在数据库zero中新建一个数据表,命名为db_account

95b754c7ac1f

新建数据表

编辑列。共有三列,id,name,password。并将id设为自增长和主键

95b754c7ac1f

编辑列

编写后台接口

在控制器Controller目录中新建名为_A_mysql的php文件,此文件的功能是php操作mysql的常用代码,这里封装成一个文件,为了减少代码的重复,和方便修改mysql的登入信息。

_A_mysql.php代码如下

$con = mysql_connect("localhost","root","xiao1234");

if (!$con){die('Could not connect:'.mysql_error());}

mysql_select_db("zero",$con);

?>

AccountController.class.php代码如下

namespace Home\Controller;

use Think\Controller;

header("Content-Type: text/html;charset=utf-8");

header('Access-Control-Allow-Origin:*');//允许跨域

class AccountController extends Controller {

public function login(){

$name = $_GET['name']; // 获取参数

$password = $_GET['password']; // 获取参数

$data = $this -> mysql_select($name);

if(!$data['password'] || $data['password'] !== $password){

$re->state = 0;

}else{

$re->state = 1;

$re->data->id = $data['id'];

}

$this->ajaxReturn($re);

}

public function addAccount(){

$name = $_GET['name']; // 获取参数

$password = $_GET['password']; // 获取参数

$result = $this -> mysql_insert($name,$password);

$this->ajaxReturn($result);

}

public function deleteAccount(){

$name = $_GET['name']; // 获取参数

$result = $this -> mysql_delete($name);

$this->ajaxReturn($result);

}

public function setPassword(){

$name = $_GET['name']; // 获取参数

$password = $_GET['password']; // 获取参数

$result = $this -> mysql_updata($name,$password);

$this->ajaxReturn($result);

}

public function mysql_insert($name,$password){

if(json_encode($this->mysql_select($name))=='false'){//是否重名

include '_A_mysql.php';

mysql_query("INSERT INTO db_account (name, password)

VALUES ('{$name}', '{$password}')");

mysql_close($con);

$re = 1;

}else{

$re = 0;

}

return $re;

}

public function mysql_delete($name){

include '_A_mysql.php';

mysql_query("DELETE FROM db_account WHERE name='{$name}'");

mysql_close($con);

return;

}

public function mysql_updata($name,$password){

include '_A_mysql.php';

mysql_query("UPDATE db_account SET password = '{$password}'

WHERE name = '{$name}' ");

mysql_close($con);

return;

}

public function mysql_select($name){

include '_A_mysql.php';

$sql = "SELECT * FROM db_account WHERE name='{$name}'";

$result = mysql_query($sql);

$row = mysql_fetch_array($result);//获取一行数据

mysql_close($con);

return $row;

}

}

访问接口

在浏览器中输入

95b754c7ac1f

网页访问接口

在打开mysql管理工具,可以看到已经多了一条数据

95b754c7ac1f

插入数据

ajax请求

打开包含有jqery.js的html页面的控制台

输入如下js代码

$.ajax({

type: "GET",

url: "http://localhost/zero/index.php/Home/Account/addAccount?",

data: {

name:'simon',

password:'simon',

},

dataType: "json",

success: function(result){

console.log('成功回调',result);

},

error: function(result){

console.log('失败回调',result);

}

});

95b754c7ac1f

Paste_Image.png

打开mysql管理工具,可以看到又多了一条记录

95b754c7ac1f

ajax请求

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值