【问题标题】:Undefined index in PHP scriptPHP脚本中未定义的索引
【发布时间】:2013-08-27 07:33:03
【问题描述】:

未定义索引:C:\wamp\www\gcm_server_php\check_user.php11
行中的 phone_no 收到此错误,但 php 代码在浏览器中工作正常并获得响应代码 200

安卓代码

 package com.pushnotification;

 import org.apache.http.HttpResponse;
 import org.apache.http.util.EntityUtils;
 import org.json.JSONObject;

 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.Toast;

 public class JsonArray extends Activity {

Button btnAsonArray;
JSONObject listOfNumbers = null;

String listNumber;
String browserString = "?phone_no=";
String url = CommonUtilities.LOCAL_CHECK_USER_URL;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.josn_array);

    btnAsonArray = (Button) findViewById(R.id.josnArray);
    btnAsonArray.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            readNumberAndNAme();

            // Log.e("", "In Onclick Methos :- " + jsonObject);
        }
    });
}

private void readNumberAndNAme() {
    try {
        Log.e("", "in Read Numbers And Name Method :- ");

        listNumber = "9465383066";
        HttpResponse re = HTTPPoster
                .doPost(url, browserString + listNumber);
        String temp = EntityUtils.toString(re.getEntity());

        Log.e("", "=======Task Comleted sucessfully ========" + temp);
        int j = 0;
        while (j != 5) {
            Toast.makeText(this, "Length of Result :- " + temp.length(),
                    Toast.LENGTH_LONG).show();
            j++;
        }

        if (temp.compareTo("SUCCESS") == 0) {

            Log.e("", "Sending complete!");

        }

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

}

}

另一个类的android

 package com.pushnotification;

 import java.io.IOException;

 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.protocol.HTTP;

 import android.util.Log;

 public class HTTPPoster {

public static HttpResponse doPost(String url, String c)
        throws ClientProtocolException, IOException {

    Log.e("", "URL from Http Poster Class :- " + url
            + "Data to send server :- " + c);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);

    request.setHeader("Content-type", "application/json");

    HttpEntity entity;
    StringEntity s = new StringEntity(c.toString());

    s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
            "application/json"));
    entity = s;
    request.setEntity(entity);

    HttpResponse response;
    response = httpclient.execute(request);

    Log.e("", "================Method Executed sucessfully================"
            + response);
    return response;
}

 }

PHP 代码

 $phone_no = $_REQUEST["phone_no"];


include_once 'GCM.php';
include_once 'db_functions.php';
$gcm = new GCM();
$db = new db_functions();
$result = $db->isPhoneExisted($phone_no);
if($result>0)
{

    $message = array("product" => "abc");


    echo "true";
}
else 
{

    $message = array("product" => "abc");


    echo "false";
 }


?>

【问题讨论】:

  • android HTTP 请求未发送任何名为“phone_no”的字段。那是因为你正在发送一个 POST 请求,因为它是一个 GET 请求(POST 不使用查询字符串!)尝试解决这个问题,也许它会解决问题
  • @STTLCU $_REQUEST 处理两个请求,即发布或获取
  • 我知道,也许 android 中的 HTTPPost 实现会丢弃查询字符串?值得检查一下。

标签: php android


【解决方案1】:

您的变量 $phone_no 未设置或定义,因此您会看到此错误

使用isset

$phone_no = $_REQUEST["phone_no"];

if(isset($phone_no)){
include_once 'GCM.php';
include_once 'db_functions.php';
$gcm = new GCM();
$db = new db_functions();
$result = $db->isPhoneExisted($phone_no);
if($result>0)
{

    $message = array("product" => "abc");


    echo "true";
}
else 
{

    $message = array("product" => "abc");


    echo "false";
 }
}else{
echo "phone no. is empty";
}

?>

【讨论】:

  • 这只是治标不治本。
  • 阅读我的 else 案例足以定义缺少的内容
【解决方案2】:

你需要设置你的$phone_no变量

if(isset($_REQUEST["phone_no"]))  { 
     $phone_no = $_REQUEST["phone_no"];
    }

【讨论】:

  • 这只是治标不治本。
  • 我坚持现状
  • Undefined variable: phone_no in C:\wamp\www\gcm_server_php\check_user.php on line 21
    得到这个错误意味着没有从 android 代码中获取电话号码,所以 android 代码中的问题可以查看我的 android 代码
猜你喜欢
  • 2011-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 2013-01-26
  • 1970-01-01
  • 2016-10-23
  • 2014-08-11
相关资源
最近更新 更多