Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within PHP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
PHP
Undefined index
Gil Carvalho
Ranch Hand
Posts: 80
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello
I working in a small project and i'm having an error Undefined index;
<?php /** * Aceita GET e POST * * Cada pedido será feito por tag * Resposta será com dados JSON /** * verifica POST request */ if (isset($_POST['tag']) && $_POST['tag'] != '') { // tag escolhida $tag = $_POST['tag']; // inclui db handler require_once 'include/DB_Functions.php'; $db = new DB_Functions(); // resposta Array $response = array("tag" => $tag, "success" => 0, "error" => 0); // verifica o tipo de tag if ($tag == 'login') { // Request type is check Login if(isset($_POST['username'])){ $username = $_POST['username']; } if(isset($_POST['password'])){ $password = $_POST['password']; } // verifica utilizador $user = $db->getUserByNameAndPassword($username, $password); if ($user != false) { // utilizador encontrado $response["success"] = 1; $response["user"]["username"] = $user["username"]; $response["user"]["password"] = $user["password"]; echo json_encode($response); } else { // utilizador não encontrado // echo json com erro = 1 $response["error"] = 1; $response["error_msg"] = "User ou password errados!"; echo json_encode($response); } } } else { echo "Acesso negado"; } ?>
In line 34 and 35
$response["user"]["username"] = $user["username"]; $response["user"]["password"] = $user["password"];
What's wrong please?
Thank you
Roel De Nijs
Sheriff
Posts: 11604
178
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Does this work?
$response["user"] = array( "username" => $user["username"], "password" => $user["password"] )
SCJA
, SCJP (
1.4
|
5.0
|
6
),
SCJD
OCAJP 7
Gil Carvalho
Ranch Hand
Posts: 80
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi
No...
Now i have this in my logCat
02-20 14:43:43.704: E/Buffer Error(22965): Error converting result java.lang.NullPointerException: lock == null
Can't get what is wrong..
And this is my log from Android
02-20 14:57:58.548: E/JSON(25742): array(6) { 02-20 14:57:58.548: E/JSON(25742): [0]=> 02-20 14:57:58.548: E/JSON(25742): string(1) "1" 02-20 14:57:58.548: E/JSON(25742): ["userId"]=> 02-20 14:57:58.548: E/JSON(25742): string(1) "1" 02-20 14:57:58.548: E/JSON(25742): [1]=> 02-20 14:57:58.548: E/JSON(25742): string(3) "Gil" 02-20 14:57:58.548: E/JSON(25742): ["utilizador"]=> 02-20 14:57:58.548: E/JSON(25742): string(3) "Gil" 02-20 14:57:58.548: E/JSON(25742): [2]=> 02-20 14:57:58.548: E/JSON(25742): string(3) "123" 02-20 14:57:58.548: E/JSON(25742): ["pass"]=> 02-20 14:57:58.548: E/JSON(25742): string(3) "123" 02-20 14:57:58.548: E/JSON(25742): } 02-20 14:57:58.548: E/JSON(25742): <br /> 02-20 14:57:58.548: E/JSON(25742): <b>Notice</b>: Undefined index: username in <b>C:\xampp\htdocs\ebspma\index.php</b> on line <b>35</b><br /> 02-20 14:57:58.548: E/JSON(25742): <br /> 02-20 14:57:58.548: E/JSON(25742): <b>Notice</b>: Undefined index: password in <b>C:\xampp\htdocs\ebspma\index.php</b> on line <b>36</b><br /> 02-20 14:57:58.548: E/JSON(25742): {"tag":"login","success":1,"error":0,"user":{"username":null,"password":null}} 02-20 14:57:58.548: E/JSON Parser(25742): Error parsing data org.json.JSONException: Value array(6) of type java.lang.String cannot be converted to JSONObject 02-20 14:57:58.558: E/AndroidRuntime(25742): FATAL EXCEPTION: main 02-20 14:57:58.558: E/AndroidRuntime(25742): java.lang.NullPointerException 02-20 14:57:58.558: E/AndroidRuntime(25742): at gmc.ebspmamobile.Login$1.onClick(Login.java:60) 02-20 14:57:58.558: E/AndroidRuntime(25742): at android.view.View.performClick(View.java:4475) 02-20 14:57:58.558: E/AndroidRuntime(25742): at android.view.View$PerformClick.run(View.java:18786) 02-20 14:57:58.558: E/AndroidRuntime(25742): at android.os.Handler.handleCallback(Handler.java:730) 02-20 14:57:58.558: E/AndroidRuntime(25742): at android.os.Handler.dispatchMessage(Handler.java:92) 02-20 14:57:58.558: E/AndroidRuntime(25742): at android.os.Looper.loop(Looper.java:137) 02-20 14:57:58.558: E/AndroidRuntime(25742): at android.app.ActivityThread.main(ActivityThread.java:5419) 02-20 14:57:58.558: E/AndroidRuntime(25742): at java.lang.reflect.Method.invokeNative(Native Method) 02-20 14:57:58.558: E/AndroidRuntime(25742): at java.lang.reflect.Method.invoke(Method.java:525) 02-20 14:57:58.558: E/AndroidRuntime(25742): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 02-20 14:57:58.558: E/AndroidRuntime(25742): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 02-20 14:57:58.558: E/AndroidRuntime(25742): at dalvik.system.NativeStart.main(Native Method)
Roel De Nijs
Sheriff
Posts: 11604
178
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Maybe
you should
first explain what you are trying to do... instead of posting some chuncks of code and let us guess about the purpose of the code
SCJA
, SCJP (
1.4
|
5.0
|
6
),
SCJD
OCAJP 7
Gil Carvalho
Ranch Hand
Posts: 80
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi
This is a login system for android.
It uses php, json e android.
Now the undefined index is resolved but i have other issues and i don't know why.
My php is this one after correcting the undefined index
<?php /** * Aceita GET e POST * * Cada pedido será feito por tag * Resposta será com dados JSON /** * verifica POST request */ if (isset($_POST['tag']) && $_POST['tag'] != '') { // tag escolhida $tag = $_POST['tag']; // inclui db handler require_once 'include/DB_Functions.php'; $db = new DB_Functions(); // resposta Array $response = array("tag" => $tag, "success" => 0, "error" => 0); // verifica o tipo de tag if ($tag == 'login') { // Request type is check Login $username = $_POST['username']; $password = $_POST['password']; // verifica utilizador $user = $db->getUserByNameAndPassword($username, $password); var_dump($user); if ($user != false) { // utilizador encontrado $response["success"] = 1; if(isset($user["username"])) { $response["user"]["username"] = $user["username"];} if(isset($user["password"])) { $response["user"]["password"] = $user["password"];} //$response["user"]["username"] = $user["username"]; //$response["user"]["password"] = $user["password"]; echo json_encode($response); } else { // utilizador não encontrado // echo json com erro = 1 $response["error"] = 1; $response["error_msg"] = "User ou password errados!"; echo json_encode($response); } } } else { echo "Acesso negado"; } ?>
And the error
02-20 12:06:05.510: E/JSON(1539): array(6) { 02-20 12:06:05.510: E/JSON(1539): [0]=> 02-20 12:06:05.510: E/JSON(1539): string(1) "1" 02-20 12:06:05.510: E/JSON(1539): ["userId"]=> 02-20 12:06:05.510: E/JSON(1539): string(1) "1" 02-20 12:06:05.510: E/JSON(1539): [1]=> 02-20 12:06:05.510: E/JSON(1539): string(3) "Gil" 02-20 12:06:05.510: E/JSON(1539): ["utilizador"]=> 02-20 12:06:05.510: E/JSON(1539): string(3) "Gil" 02-20 12:06:05.510: E/JSON(1539): [2]=> 02-20 12:06:05.510: E/JSON(1539): string(3) "123" 02-20 12:06:05.510: E/JSON(1539): ["pass"]=> 02-20 12:06:05.510: E/JSON(1539): string(3) "123" 02-20 12:06:05.510: E/JSON(1539): } 02-20 12:06:05.510: E/JSON(1539): {"tag":"login","success":1,"error":0} 02-20 12:06:05.510: E/JSON Parser(1539): Error parsing data org.json.JSONException: Value array(6) of type java.lang.String cannot be converted to JSONObject 02-20 12:06:05.510: D/Aqui(1539): antes 02-20 12:06:05.520: D/Aqui(1539): passei 02-20 12:06:05.530: W/System.err(1539): org.json.JSONException: No value for user 02-20 12:06:05.550: W/System.err(1539): at org.json.JSONObject.get(JSONObject.java:354) 02-20 12:06:05.550: W/System.err(1539): at org.json.JSONObject.getJSONObject(JSONObject.java:573) 02-20 12:06:05.550: W/System.err(1539): at gmc.ebspmamobile.Login$1.onClick(Login.java:69) 02-20 12:06:05.560: W/System.err(1539): at android.view.View.performClick(View.java:4240) 02-20 12:06:05.560: W/System.err(1539): at android.view.View$PerformClick.run(View.java:17721) 02-20 12:06:05.570: W/System.err(1539): at android.os.Handler.handleCallback(Handler.java:730) 02-20 12:06:05.570: W/System.err(1539): at android.os.Handler.dispatchMessage(Handler.java:92) 02-20 12:06:05.579: W/System.err(1539): at android.os.Looper.loop(Looper.java:137) 02-20 12:06:05.579: W/System.err(1539): at android.app.ActivityThread.main(ActivityThread.java:5103) 02-20 12:06:05.590: W/System.err(1539): at java.lang.reflect.Method.invokeNative(Native Method) 02-20 12:06:05.600: W/System.err(1539): at java.lang.reflect.Method.invoke(Method.java:525) 02-20 12:06:05.600: W/System.err(1539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 02-20 12:06:05.600: W/System.err(1539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 02-20 12:06:05.610: W/System.err(1539): at dalvik.system.NativeStart.main(Native Method)
The Json parse doesn't work....
Roel De Nijs
Sheriff
Posts: 11604
178
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
But which json is invalid? I assume the var_dump of the user.
SCJA
, SCJP (
1.4
|
5.0
|
6
),
SCJD
OCAJP 7
Gil Carvalho
Ranch Hand
Posts: 80
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
No
var_dump($user); prints the following data:
array(6) { [0]=> string(1) "1" ["userId"]=> string(1) "1" [1]=> string(3) "Gil" ["utilizador"]=> string(3) "Gil" [2]=> string(3) "123" ["pass"]=> string(3) "123" }
so the query works, the problem is here, response variable seem to be empty...because i don't receive any data in android app
if ($user != false) { $response["success"] = 1; if(isset($user["username"])) { $response["user"]["username"] = $user["username"];} if(isset($user["password"])) { $response["user"]["password"] = $user["password"];} echo json_encode($response);
so what i'm doing wrong?
Thank you
Roel De Nijs
Sheriff
Posts: 11604
178
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Based on the exception message
Value array(6) of type java.lang.String cannot be converted to JSONObject
, it seems to be about the var_dump of user, that's an array of 6 elements (and not a json object)
SCJA
, SCJP (
1.4
|
5.0
|
6
),
SCJD
OCAJP 7
Gil Carvalho
Ranch Hand
Posts: 80
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The var_dump was just to check the values, if i remove it i get this error in android app
02-20 12:06:05.530: W/System.err(1539): org.json.JSONException: No value for user 02-20 12:06:05.550: W/System.err(1539): at org.json.JSONObject.get(JSONObject.java:354) 02-20 12:06:05.550: W/System.err(1539): at org.json.JSONObject.getJSONObject(JSONObject.java:573) 02-20 12:06:05.550: W/System.err(1539): at gmc.ebspmamobile.Login$1.onClick(Login.java:69) 02-20 12:06:05.560: W/System.err(1539): at android.view.View.performClick(View.java:4240) 02-20 12:06:05.560: W/System.err(1539): at android.view.View$PerformClick.run(View.java:17721) 02-20 12:06:05.570: W/System.err(1539): at android.os.Handler.handleCallback(Handler.java:730) 02-20 12:06:05.570: W/System.err(1539): at android.os.Handler.dispatchMessage(Handler.java:92) 02-20 12:06:05.579: W/System.err(1539): at android.os.Looper.loop(Looper.java:137) 02-20 12:06:05.579: W/System.err(1539): at android.app.ActivityThread.main(ActivityThread.java:5103) 02-20 12:06:05.590: W/System.err(1539): at java.lang.reflect.Method.invokeNative(Native Method) 02-20 12:06:05.600: W/System.err(1539): at java.lang.reflect.Method.invoke(Method.java:525) 02-20 12:06:05.600: W/System.err(1539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 02-20 12:06:05.600: W/System.err(1539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 02-20 12:06:05.610: W/System.err(1539): at dalvik.system.NativeStart.main(Native Method)
org.json.JSONException: No value for user
So....i'm lost
Roel De Nijs
Sheriff
Posts: 11604
178
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
And how does the json-object looks like that you are returning?
SCJA
, SCJP (
1.4
|
5.0
|
6
),
SCJD
OCAJP 7
Gil Carvalho
Ranch Hand
Posts: 80
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Json should return
{ "tag": "login", "success": 1, "error": 0, "user": { "utilizador": "Gil", "pass": "123" } }
Roel De Nijs
Sheriff
Posts: 11604
178
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Gil Carvalho wrote:
Json should return
Does your php code returns this json?
SCJA
, SCJP (
1.4
|
5.0
|
6
),
SCJD
OCAJP 7
Gil Carvalho
Ranch Hand
Posts: 80
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
No
Returns
{"tag":"login",
"success":1,
"error":0
}
Roel De Nijs
Sheriff
Posts: 11604
178
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
That explains your error, because there is no user in the json-string and you try to retrieve the "user" key as a JSONObject.
SCJA
, SCJP (
1.4
|
5.0
|
6
),
SCJD
OCAJP 7
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Redirecting to a login page with error messages
php database addition problem
difficulty in login in one attempt
session login hashed password
Unable to fetch correct json string
More...