<?php if(empty($_POST['phone'])){ //這個為中文的時候 資料庫亂碼 return_json(1,'手機號碼不能為空'); } if(empty($_POST ['pwd'])){ return_json(2,'密碼不能為空'); } $phone = find('php_user','*','phone='.$_POST['phone']); if( $phone){ return_json(3,'手機號碼已存在'); } $arr = array( 'phone' => $_POST['phone'], 'pwd' => md5($_POST['pwd' ]), 'add_time' => time(), 'nickname' => isset($_POST['nickname']) ? $_POST['nickname'] : $_POST['phone'], 'sex' = > isset($_POST['sex']) ? $_POST['sex'] : 0, 'avatar' => isset($_POST['avatar']) ? $_POST['avatar'] : '' ); $add = insert('php_user',$arr); return_json(0,'註冊成功'); function connect(){ $db = array( 'charset' => 'utf8', 'charset' => 'utf8', 'charset' => 'utf8', port> ; 3306, 'type' => 'mysql', 'host' => '127.0.0.1', 'name' => ; 'hao1' ); $dsn = "{$db['type']}:host={$db['host']}; dbname={$db['name']}; charset={$db[ 'charset']}; port={$db['port']}" try { //實例化PDO類別,建立PDO物件 $pdo = new PDO($dsn,$db['user'],$db[ 'pass']); } catch (PDOException $e) { die('資料庫錯誤:'.$e->getMessage()); } return $pdo;}function insert($table,$data=[pdo;}function insert($table,$data=[] { //連接pdo $pdo = connect(); //連接pdo $pdo = connect(); //連接pdo $pdo = connect(); //連結pdo $pdo = connect(); //建立SQL語句 $sql = "INSERT INTO {$table} SET "; //組裝插入語句 if(is_array($data)){ • for$ k=>$v) { $sql .= $k.'="'.$v.'", '; } }else{ rtrim(trim($sql),',').';'; //建立PDO預處理物件 $stmt = $pdo->prepare($sql); //執行新增作業 if($stmt-> ;execute()){ if($stmt->rowCount()>0){ return true; } }else { 返回 false; }}