gpt4 book ai didi

php - 在 flutter signup php & mysql 中检查注册用户

转载 作者:IT王子 更新时间:2023-10-29 07:13:14 27 4
gpt4 key购买 nike

我有 flutter 应用程序,通过 php 和 mysql 检查用户注册。我在 php 中搜索,如:

 $sqlUser = "SELECT * FROM users WHERE username = '".$username."'";
$rs = mysqli_query($con ,$sqlUser);
$numUsers = mysqli_num_rows($rs);

if($numUsers > 0) {
echo "User already exists<br/>";
}

这是我创建用户的 php 脚本:

 <?php
require_once 'connectdb.php';

$user_id= $_POST['id'];
$username = $_POST['email'];
$password = $_POST['password'];
$nama = $_POST['name'];
$nik = $_POST['nik'];
$lahir = $_POST['born'];
$tentang = $_POST['about'];
$pengalaman = $_POST['exp'];
$linkedin = $_POST['linkedin'];


$query="INSERT INTO user_info(id, email, password, name, nik, born, about, exp,linkedin)
VALUES ('$user_id','$username','$password','$name','$nik', '$born', '$about', '$exp', '$linkedin')";
$exeQuery = mysqli_query($con, $query) ;

if($exeQuery){
echo (json_encode(array('code' =>1, 'message' => 'Ajouter avec succée')));
}else {echo(json_encode(array('code' =>2, 'message' => 'Non Terminer')));
}


?>

我的 flutter 注册:

void _register() async {
setState(() {
_isLoading = true;
});
var url = "url/newuser.php";

await http.post(url, body: {
"email": _emailController.text,
"password": _passwordController.text,
"name": _nameController.text,
"nik": _nikController.text,
});
setState(() {
_isLoading = false;
});
onCreatedAccount();
//print(_adresseController.text);
}

但是我如何将它传递给我的 flutter 应用程序,以便我的应用程序知道它已经注册了电子邮件/用户?

最佳答案

我建议你使用 Dio 库 Flutter Dio Library并使用响应从服务器获取数据。

Dio dio = new Dio();
FormData formData = new FormData.from({
"email": _emailController.text,
"password": _passwordController.text,
"name": _nameController.text,
"nik": _nikController.text,
});
dio
.post("http://yoururl.com/register.php",
data: formData,
options: Options(
method: 'POST',
responseType: ResponseType.PLAIN // or ResponseType.JSON
))
.timeout(Duration(seconds: 15))
.then((response) {
Map<String, dynamic> data = jsonDecode(response.data);
if (data['code'] == '1') {
//success code
}else{
//error code
}

在服务器中:

$response = array();
//do sql test and storing
if(success){
$response['code'] = '1';
$response['message'] = 'success message.';
}else{
$response['code'] = '2';
$response['message'] = 'error message.';
}
echo json_encode($response);

关于php - 在 flutter signup php & mysql 中检查注册用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52933399/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com