如何使用php API解决flutter应用程序登录错误

问题描述 投票:0回答:1

我使用API在flutter应用程序中登录,并且操作完成得非常成功,但是当我为客户端添加获取数据以获取ID和名称并在页面之间传输时,发生了错误套接字期望。

     include_once "database.php";
    include_once "cores.php";

    $phone = $_POST['phone'];

    $select = "SELECT * FROM `client` WHERE `phone`='$phone' AND `status`='Active' ";
    $run = mysqli_query($connect, $select);


    if (mysqli_num_rows($run) > 0) 
    {
        $userData = array();

        while ($row = mysqli_fetch_assoc($run))
        {
            $userData = $row;
        }


        echo json_encode(
            array(
                "check" => true, 
                "userID" => $userData[0]
            )
        );

    } 
    else 
    {
        echo json_encode(array("check" => false));
    }

并且颤抖

  Future<void> LoginClient() async
  {

    if (phone.text.isNotEmpty) 
    {
      try 
      {

        var validate = await http.post(
          Uri.parse(ApisConnect.checkPhoneNumberApi),
          body: {
            "phone" : phone.text
          }
        );

        var response1 = jsonDecode(validate.body);

        if (response1["exists"] == true) 
        {
          var login = await http.post(
          Uri.parse(ApisConnect.loginApi),
            body: {
              "phone": phone.text,
            },
          );

          var response2 = jsonDecode(login.body);

          if (response2["check"] == true) 
          {
            Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => DashboardScreen()));

            User userInfo = User.fromJson(response2["userID"]);
            
            Fluttertoast.showToast(msg: 'Logined Successfully,'); 
          }
          else if(response2['check'] == false)
          {
            Fluttertoast.showToast(msg: 'You Are blocked, If you have any error call Techneical Support');
          }

        }
        else
        {
          Fluttertoast.showToast(msg: 'This Number not signed, sign up first.');
        }

      } 
      catch (e) 
      {
        Fluttertoast.showToast(msg: 'Error: $e');
      }       
    }
    else
    {
      Fluttertoast.showToast(msg: 'Fill The field first');
    }

小心,当我在flutter中添加用户行之前在php中添加userID时发生错误,添加后仍然发现错误

php flutter mobile
1个回答
0
投票

由于您使用

mysqli_fetch_assoc
,SQL 操作的结果将是一个关联数组。所以
$userData[0]
不起作用,您需要使用数据库中的列名称。

例如

client
表中的用户id是
userId
,正确的获取方式是:

$userData["userId"]

我假设

$connect
来自包含的文件之一,您可能需要
global $connect;
include_once
语句之后。

除了您当前的代码对 SQL 注入存在危险的开放性之外,每次您的 SQL 命令包含任何用户输入时,请考虑使用 prepared statements

© www.soinside.com 2019 - 2024. All rights reserved.