【问题标题】:PHP API returning wrong response for android appPHP API为Android应用返回错误响应
【发布时间】:2016-01-18 10:43:12
【问题描述】:

我正在为PHP 中的android 开发人员创建一个API,他想在其中从数据库中删除一些值并希望在此之后显示一条消息。 现在的问题是该数据已成功删除,但此 API 在完成该过程后始终显示其他部分消息。如果我删除 else 部分,它会返回 null,这会使 android 应用程序崩溃。所以我只想给android开发者一个正确的json消息

这是我正在尝试的代码

 if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes"  && $user_name_id == $user_name_id1)
        {               
            $tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");

            foreach($tables_count as $table_count) 
            {

                $user_count = mysql_query("select * from $table_count where user_name = '$user_name'");
                $total_user_count = mysql_num_rows($user_count);

                if($total_user_count > 0)
                {

                    $tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
                    foreach($tables_data as $table_data) 
                    {
                        $user_sql = mysql_query("delete from $table_data where user_name='$user_name'");

                        if($user_sql)
                        {
                            $response['success'] = 1;
                            $response['user']['error_msg'] = 'Clear Successfully All History!';             
                        }
                    }
                }
                else
                {

                    $response['success'] = 0;
                    $response['user']['error_msg'] = 'Record Not Found!';
                }



            }       
        }

我知道这个逻辑有问题。但我需要专家建议我的逻辑哪里错了,我必须做些什么才能让它成功

【问题讨论】:

  • 试试这个: if($total_user_count > 0) { $tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements"); foreach($tables_data as $table_data) { $user_sql = mysql_query("从 $table_data 中删除 user_name='$user_name'"); } $response['成功'] = 1; $response['user']['error_msg'] = '成功清除所有历史记录!'; } 并删除 else 部分

标签: php android api


【解决方案1】:

您的原始代码的问题是您在循环内设置成功/失败。四个表之一可能/可能不包含用户名。如果最后一个表没有,那么根据您的逻辑,即使循环的先前迭代从存在用户名的表中删除了数据,您也会得到“找不到记录”。

<?php
    $conn = mysqli_connect(.....);
     if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes"  && $user_name_id == $user_name_id1) {               
                $tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
                $userHistoryDeleted = 0;
                foreach($tables_count as $table_count) {
                     //if history is found, then it will be deleted otherwise not
                     mysql_query("delete from $table_count where user_name = '$user_name'");
                     if(mysqli_affected_rows($conn)) {
                          $userHistoryDeleted = 1;
                     }

                }
                $msg = 'Record Not Found!';
                if($userHistoryDeleted) {
                     $msg = 'Clear Successfully All History!';
                }
                $response['success'] = $userHistoryDeleted;
                $response['user']['error_msg'] = $msg;
    }

【讨论】:

    【解决方案2】:

    更改您的代码:

    if($total_user_count > 0)
                    {
    
                        $tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
                        foreach($tables_data as $table_data) 
                        {
                            $user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
    
                            if($user_sql)
                            {
                                $response['success'] = 1;
                                $response['user']['error_msg'] = 'Clear Successfully All History!';             
                            }
                        }
                    }
                    else
                    {
    
                        $response['success'] = 0;
                        $response['user']['error_msg'] = 'Record Not Found!';
                    }
    

    到这个

    if($total_user_count > 0)
                    {
    
                        $tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
                        foreach($tables_data as $table_data) 
                        {
                            $user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
    
                        }
                        $response['success'] = 1;
                        $response['user']['error_msg'] = 'Clear Successfully All History!';
                    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-09
      • 2019-05-26
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2021-12-20
      • 1970-01-01
      相关资源
      最近更新 更多