问题描述
phpif(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', 'port' => 3306, 'type' => '', 'host' => '127.0.0.1', 'user' => 'hao2', 'pass' => 'hao3', '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 $pdo = connect(); //创建sql语句 $sql = "insert into {$table} set "; //组装插入语句 if(is_array($data)){ foreach ($data as $k=>$v) { $sql .= $k.'="'.$v.'", '; } }else{ return false; } //去掉尾部逗号,并添加分号结束 $sql = rtrim(trim($sql),',').';'; //创建pdo预处理对象 $stmt = $pdo->prepare($sql); //执行新增操作 if($stmt->execute()){ if($stmt->rowcount()>0){ return true; } } else { return false; }}
问题解答
回答1:看看你数据库的编码方式和你php中的编码方式