当前位置: 首页 > 知识库问答 >
问题:

PHP if else实体都在执行

慕高格
2023-03-14

我是PHP的初学者,我有这样的代码,如果和其他部分都在同一个“运行”中执行。我知道这在逻辑上是不可能的,但这就是我得到的。

以下是我的PHP代码:

<?php

require_once 'create_request_form.php';

$db = new create_request_form();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['rqTitle']) && isset($_POST['rqMname']) && isset($_POST['rqUname']) && isset($_POST['rqBranch']) && isset($_POST['rqText']) && isset($_POST['rqMinPrice']) && isset($_POST['rqMaxPrice']) && isset($_POST['rqImage']) && isset($_POST['rqCategory']) && isset($_POST['rqDateTime'])) {

    // receiving the post params
    $rqTitle = $_POST['rqTitle'];
    $rqMname = $_POST['rqMname'];
    $rqUname = $_POST['rqUname'];
    $rqBranch = $_POST['rqBranch'];
    $rqText = $_POST['rqText'];
    $rqMinPrice = $_POST['rqMinPrice'];
    $rqMaxPrice = $_POST['rqMaxPrice'];
    $rqImage = $_POST['rqImage'];
    $rqCategory = $_POST['rqCategory'];
    $rqDateTime = $_POST['rqDateTime'];


    // check if there is a request  with the same title
    if ($db->checkReqTitle($rqTitle)) {
        // Request already exists
        $response["error"] = TRUE;
        $response["error_msg"] = "Request already exists with the title: " . $rqTitle;
        echo json_encode($response);
    } else {
        // create a new request
        $request = $db->StoreReqInfo($rqTitle, $rqMname, $rqUname, $rqBranch, $rqText, $rqMinPrice, $rqMaxPrice, $rqImage, $rqCategory, $rqDateTime);

        if ($request) {
            // request stored successfully
            $response["error"] = FALSE;
            $response["request"]["rqTitle"] = $request["rqTitle"];
            $response["request"]["rqMname"] = $request["rqMname"];
            $response["request"]["rqUname"] = $request["rqUname"];
            $response["request"]["rqBranch"] = $request["rqBranch"];
            $response["request"]["rqText"] = $request["rqText"];
            $response["request"]["rqMinPrice"] = $request["rqMinPrice"];
            $response["request"]["rqMaxPrice"] = $request["rqMaxPrice"];
            $response["request"]["rqImage"] = $request["rqImage"];
            $response["request"]["rqCategory"] = $request["rqCategory"];
            $response["request"]["rqDateTime"] = $request["rqDateTime"];

            echo json_encode($response);

        } else {
            // request failed to store
            $response["error"] = TRUE; 
            $response["error_msg"] = "An error occurred while creating the request. Please try again.";
            echo json_encode($response);
        }
     }
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameter is missing!";
    echo json_encode($response);
}
?>

所需的行为(在数据库中存储数据)都执行得很好,但是我得到的$响应是当请求标题已经存在时分配给我的错误字符串,而不是存储请求的JSON格式化数组(如果执行和其他机构,但是我得到如果身体作为反应的结果)。

我还使用Galaxy Note 4进行测试,它只显示一条空白的Toast消息,不会启动intent(不会移动到主屏幕)。

到目前为止,唯一可能的解释是脚本被执行了两次,但是我找不到第二次调用它的地方。

这是java代码,负责将带有所需参数的请求发送到上面的php脚本。

 private void storeRequest(final String rTitle, final String rMname, final String rUname,
                          final String rBranch, final String rText,  final String rMinPrice,
                          final String rMaxPrice, final String imagePath, final String rCategory) {

    // Tag used to cancel the request
    String cancel_req_tag = "request";

    //A progress dialog message to let the user know they are being registered
    progressDialog.setMessage("Please wait while we add your request...");
    showDialog();

    //creating a StringRequest to send the registration info to the script
    // at the server for processing
    StringRequest strReq = new StringRequest(Request.Method.POST,
            URL_FOR_REQUEST, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Request Response: " + response.toString());
            hideDialog();

            try { //json objects must be surrounded by try catch statement
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                if (!error) { // error is set to false, meaning no error

                    // Launch home activity
                    Intent intent = new Intent(CreateRequestActivity.this, HomeActivity.class);
                    startActivity(intent);
                    finish();

                } else {

                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(CreateRequestActivity.this, errorMsg, Toast.LENGTH_LONG).show();
                }



            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
            Toast.makeText(CreateRequestActivity.this,
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("rqTitle", rTitle);
            params.put("rqText", rText);
            params.put("rqMname", rMname);
            params.put("rqUname", rUname);
            params.put("rqBranch", rBranch);
            params.put("rqMinPrice", rMinPrice);
            params.put("rqMaxPrice", rMaxPrice);
            params.put("rqImage", imagePath);
            params.put("rqCategory", rCategory);
            params.put("rqDateTime", DateFormat.getDateTimeInstance().format(new Date()));
            return params;
        }
    };
    // Adding request to request queue
    AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);
}

有什么想法吗?

提前感谢!

共有1个答案

冷英光
2023-03-14

因此,问题源于Java代码,而不是PHP(糟糕的PHP;P)。更准确地说,截击请求存在请求超时问题,特别是在使用POST方法发送请求时。

所以对我来说有效的是在将请求添加到队列之前添加这一行:

strReq.setRetryPolicy(new DefaultRetryPolicy(0, -1, 0));

*用您的请求名称替换strReq。基本上,这条线的作用是防止截击在第一个请求花费太长时间时发送重复的请求。

原著

DefaultRetryPolicy.class的hasAttemptRemning()类如下所示:

受保护的布尔值hasAttemptRemning(){返回this. mMONtRetryCount

我用螺丝钉把它修好了

要求setRetryPolicy(新的DefaultRetryPolicy(0,-1,0);

来源:当重试策略设置为0时,Android Volley向服务器发出2个请求

 类似资料:
  • 问题内容: 我正在使用带有实体框架5的asp.net mvc3。我有.edmx文件,并能够使用linq或SP与数据库交互,但是我想运行一些原始sql语句。我正在尝试这样的事情: 是否有可能以这种方式执行sql查询?谢谢。 问题答案: 您可以执行以下类型的查询: 对实体类型的SQL查询,该查询返回特定类型的实体。 { } 非实体类型的SQL查询,该查询返回原始数据类型。 { var studentN

  • 我正在尝试用Spring 3、JPA 2和Hibernate 3制作一个应用程序。我有一个问题,当你坚持一个实体:什么都没发生!数据不插入数据库,也不执行查询。但是,当我使用一个请求,如query.getResultList()选择正常工作。 所以我认为我的问题只是在坚持/更新和事务管理器上,但我不太擅长Spring。你能帮帮我吗? 下面是我的配置文件: 谁能帮帮我吗?

  • 路由执行体 Tango 支持 5 种形式的函数或结构体方法作为执行体: func() func(http.ResponseWriter, *http.Request) func(*tango.Context) func(http.Response.Writer) func(*http.Request) struct.Get() func() t := tango.Classic() t.Get("

  • 我很好奇,在JPA/Hibernate的父实体中,是否可能有几个相同实体的@manytone关系。 示例:我有银行交易,每笔交易都有一个银行合作伙伴,分别担任债权人和债务人。关键是,我只想编辑一次数据。昵称为“情妇”的银行合作伙伴只有一个:),无论是债权人还是债务人。一旦,它将重命名为妻子,所以我不想单独更改。此外,余额是BankPartners在这两个角色中的所有交易的总和。 @实体公共类事务{

  • 我在研究手机导航。我对简单的if/else如何工作的理解肯定有根本性的错误(我已经有一段时间没有使用jQ/js了),因为这两个语句是串行执行的。实际上‘else’首先运行,然后——不管我是否包含‘toggleNav()’函数或$(nav)。toggleClass('open') -然后也执行' if '语句。 我认为这可能与事件有关,所以我尝试了两个li上的函数。仪表板切换导航和(a.dashbo

  • 问题内容: 我将这段代码与从文中)获得的声明一起使用,它的工作原理绝对不错: 但是,如果我将其更改为以下内容,则会中断,因为除了执行所有其他情况外: 当我通过时会打印出来。我尝试将生成器更改为缓冲,但是得到了相同的响应…这可能是错误,还是我犯了一些错误? 问题答案: 这是您代码中的错误。您忘了每次输入: