<?php
//設(shè)置json
header('Content-type:text/json');
header('charset=utf-8');
//設(shè)置錯(cuò)誤層級沐扳,解決參數(shù)未傳時(shí)異常的情況
error_reporting(E_ALL^E_NOTICE);
if(isset($_SERVER["HTTP_ORIGIN"])) {//跨域配置
header('Access-Control-Allow- Origin:'.$_SERVER["HTTP_ORIGIN"]);
}
//header('Access-Control-Allow-Origin: *');
//方法
header('Access-Control-Allow-Methods:OPTIONS,POST,GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
//緩存時(shí)間
header('Access-Control-Max-Age:86400');
//跨域Ajax請求時(shí)是否帶Cookie的設(shè)置
header('Access-Control-Allow-Credentials:true');
//返回json數(shù)據(jù)中的字段蚓炬。
$data['code']=0;
$data['msg']='存儲失敗';
//默認(rèn)主機(jī)(這里數(shù)據(jù)你自己數(shù)據(jù)庫信息)
define('host','127.0.0.1');
define('user','root');
define('passwd','root');
define('dbName', 'arman');
//連接
$handle = mysqli_connect(host,user,passwd,dbName);
if(!$handle){//連接失敗
$data['code']=0;
$data['msg']='連接數(shù)據(jù)庫失敗';
//JSON_UNESCAPED_UNICODE解決中文亂碼,并通過json_encode來轉(zhuǎn)換json
echo json_encode($data,JSON_UNESCAPED_UNICODE);
return;
}
//接收參數(shù)
$uname = $_POST['uname'];
$phone = $_POST['phone'];
$nickname = $_POST['nickname'];
//處理參數(shù)中的空格
// $uname_ = htmlspecialchars(stripcslashes(trim($uname)));
// $phone_ = htmlspecialchars(stripcslashes(trim(phone)));
// $nickname_ = htmlspecialchars(stripcslashes(trim(nickname)));
if(!empty($uname)&&!empty($phone)&&!empty($nickname)){
//存儲過程
$insert = "CALL save('$uname','$phone','$nickname');";
//執(zhí)行存儲過程
$do = mysqli_query($handle,$insert);
//取存儲過程返回的結(jié)果
$result = mysqli_fetch_assoc($do);
//釋放資源
mysqli_free_result($do);
//關(guān)閉連接
mysqli_close($handle);
if($result['code']>0){
$data['code']=1;
$data['msg']='投票成功';
}else{
$data['code']=0;
$data['msg']='投票失敗';
}
echo json_encode($data,JSON_UNESCAPED_UNICODE);
}else{
$data['code']=0;
$data['msg']='參數(shù)錯(cuò)誤';
echo json_encode($data,JSON_UNESCAPED_UNICODE);
}
?>
存儲過程非常簡單殿遂,登入的話自己實(shí)現(xiàn)一下。
BEGIN
INSERT INTO `armani` (uname,phone,nickname) VALUES(_uname,_phone,_nickname);
SELECT ROW_COUNT() as 'code';
END