I'm making android app with communication to PHP MySQL
there is PHP files
create_order.php
get_all_orders.php
get_order_status.php
create_order.php works
get_all_orders.php also
but example in browser I'm typing get_order_status.php?oid=102 it gives me response but it contains an error
{"success":0,"message":"Required field(s) is missing get status"} when trying it from android by POST method
RequestQueue queue = MyVolley.getRequestQueue();
String num1 = "1";
if (num1 != null && !num1.equals("")) {
String uri = String
.format("http://192.168.0.101/android_connect/get_order_status.php?param1=%1$s",
num1);
StringRequest myReq = new StringRequest(Request.Method.GET,
uri,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
}
get_order_status.php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
//require_once __DIR__ . '/db_connect.php';
// connecting to db
//$db = new DB_CONNECT();
// check for post data
//echo("post data - ".$_GET['oid']);
if (isset($_GET['oid'])) {
$pid = $_GET['oid'];
// get a order from orders table
$mysqli = new mysqli('localhost','root','rootpass','testdb');
$result = mysqli_fetch_array($mysqli->query("SELECT * FROM orders WHERE oid = $pid"));
//echo($result["status"]);
if (count($result)>0) {
// check for empty result
$order = array();
$order ["status"] = $result["status"];
// success
$response["success"] = 1;
// user node
$response["order"] = array();
array_push($response["order"], $order);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No order found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing get status";
// echoing JSON response
echo json_encode($response);
}
?>
change param1 to oid in this line (android code)
String uri = String.format("http://192.168.0.101/android_connect/get_order_status.php?param1=%1$s",num1);
should be like this:
String uri = String.format("http://192.168.0.101/android_connect/get_order_status.php?oid=%1$s",num1);
as in your PHP file you are reading parameter named oid
$pid = $_GET['oid'];
Related
I have a php script with this output:
And then I display this data in Android. But now, I put a .htaccess and .htpasswd file in the directory and I have to enter a password, if I want to read the data. My question is how can I display the data in Android, now with the files (.htaccess and .htpasswd). Is there a way to enter the password in Android to show the data because now I cant display the data..
Thanks
EDIT:
This is my php script:
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT * FROM users") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["feed"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$feed = array();
$feed["uid"] = $row["uid"];
$feed["unique_id"] = $row["unique_id"];
$feed["name"] = $row["name"];
$feed["full_name"] = $row["full_name"];
$feed["email"] = $row["email"];
$feed["age"] = $row["age"];
$feed["about_the_customer"] = $row["about_the_customer"];
$feed["encrypted_password"] = $row["encrypted_password"];
$feed["salt"] = $row["salt"];
$feed["created_at"] = strtotime($row["created_at"]);
$feed["updated_at"] = $row["updated_at"];
$feed["imageName"] = $row["imageName"];
// push single product into final response array
array_push($response["feed"], $feed);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No feed found";
// echo no users JSON
echo json_encode($response);
}
?>
How do i add register validation so that user wont be able to use the same user id as others? I'm stuck here as I have tried every code and nothing would work. And it will crash my app.
I'm using Eclipse to do my app.
My php file
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['user_name']) && isset($_POST['user_pwd'])){
$user_name = $_POST['user_name'];
$user_pwd = $_POST['user_pwd'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO doc_user (user_name, user_pwd) VALUES('$user_name', '$user_pwd')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
change this part in your code
// connecting to db
$db = new DB_CONNECT();
// chack database
$result = mysql_query("select user_name from doc_user where user_name = '$user_name'");
if(mysql_num_rows($result)){
$response["success"] = 0;
$response["message"] = "username not available.";
// echoing JSON response
echo json_encode($response);
exit;
}
// mysql inserting a new row
$result = mysql_query("INSERT INTO doc_user (user_name, user_pwd) VALUES('$user_name', '$user_pwd')");
Before listing out the items, I have a function of the app to create a new data where it includes a unique id from another activity. The unique data is "sid". I used PHP to connect to a phpmyadmin database.
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['foodName']) && isset($_POST['foodPrice']) && isset($_POST['foodType']) && isset($_POST['sid']) {
$foodName = $_POST['foodName'];
$foodPrice = $_POST['foodPrice'];
$foodType = $_POST['foodType'];
$sid = $_POST['sid'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO food(foodName, foodPrice, foodType, sid) VALUES('$foodName', '$foodPrice', '$foodType', '$sid')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Food successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
And this is the intent to receive the data from the other activity :
Intent i = getIntent();
// getting product id (sid) from intent
sid = i.getStringExtra(TAG_SID);
Lastly, I have the this in the class when it is executed :
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("foodName", getFoodName));
params.add(new BasicNameValuePair("foodPrice", getFoodPrice));
params.add(new BasicNameValuePair("foodType", getFoodType));
params.add(new BasicNameValuePair("sid", sid));
Is there something that I did wrong somewhere? On my table I have this "sid" too.
you have to do the following
in the first Activity
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("sid", VALUE);
startActivity(intent);
in second activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("sid");
}
I have 2 database tables
main_menu which has 3 columns id, item_name, img
sub_menu which has columns id, sub_item_name, item_name, add_price, img
Now I am trying to get all sub menu under master menu when user clicks on master menu
eg.
select * from sub_menu where item_name="Cafe"
it should display all sub menu under Cafe.
And here is the web service I wrote to do that and get data in json format
<?php
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["item_name"])) {
// get a product from products table
$result = mysql_query("SELECT * FROM sub_menu WHERE item_name ='".$_GET["item_name"]."'");
echo $result;
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$sub_menu = array();
$sub_menu["id"] = $result["id"];
$sub_menu["item_name"] = $result["item_name"];
$sub_menu["sub_item_name"] = $result["sub_item_name"];
$sub_menu["add_price"] = $result["add_price"];
$sub_menu["img"] = $result["img"];
// success
$response["success"] = 1;
// user node
$response["sub_menu"] = array();
array_push($response["sub_menu"], $sub_menu);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No sub menu found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No menu found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
Now the problem is I am only getting first data only, it should display all data
Query is working fine in mysql, I checked that. The problem is somewhere in my php service.
You have to call mysql_fetch_array() in a loop, and add all the rows to an array. You can just copy the whole row of results in a single assignment, since you were using the same field names in your JSON as in the database.
if (mysql_num_rows($result) > 0) {
$submenu = array();
while ($result = mysql_fetch_array($result);
$submenu[] = $result;
}
$response["success"] = 1;
// user node
$response["sub_menu"] = $all_results;
// echoing JSON response
echo json_encode($response);
In my android app.. a result is taken from server and displayed in app.. where two languages are there for user to select.. English and Hindi.. The english part is working fine and displaying correctly. .but for hindi text ,... ony '???????' is displaying insted of fonts... But when I saved hindi fonts in server ..it is displaying as hindi fonts olnly ... I am using php code for connecting with the server..do we need to change it to utf8.. do we need to change any thing in php file.. I am giving my php code and sql code below.. please check and if there any error pls help..
php
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["pinnum"]))
{
$pinnum = $_GET['pinnum'];
// get a product from products table
$result = mysql_query("SELECT *FROM pin1h WHERE pinnum = $pinnum");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["pid"] = $result["pid"];
$product["pinnum"] = $result["pinnum"];
$product["pinnacle"] = $result["pinnacle"];
$product["created_at"] = $result["created_at"];
$product["updated_at"] = $result["updated_at"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
sql code
CREATE TABLE pin1h(
pid int(11) primary key auto_increment,
pinnum int(11),
pinnacle text,
created_at timestamp default now(),
updated_at timestamp
);
Just a tought but i think you should check the encoding on the server to see if what you send is alright, because i had the same problem one time and it turned out the server was sending the bad string.
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
mysql_query("SET NAMES 'utf8'");
// check for post data
if (isset($_GET["pinnum"]))
{
$pinnum = $_GET['pinnum'];
// get a product from products table
$result = mysql_query("SELECT *FROM pin1h WHERE pinnum = $pinnum");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["pid"] =$result["pid"];
$product["pinnum"] = $result["pinnum"];
$product["pinnacle"] = $result["pinnacle"];
$product["created_at"] = $result["created_at"];
$product["updated_at"] = $result["updated_at"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>