pass variable though URL for getting JSON - php

I have an app which loads data from a server and publish that data in the text view but there is a scene
<?php
define('HOST','xxxxxx');
define('USER','xxxxxxx');
define('PASS','xxxxx');
define('DB','xxxxxxx');
//Connecting to Database
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
$sql = "select * from Leaderboard where email='test#test.com'";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('name'=>$row[1],
'rank'=>$row[2],
'accuracy'=>$row[3]));
}
echo json_encode (array("list"=>$result));
mysqli_close($con);
?>
this is my PHP file which gives JSON like this
{"list":[{"name":"Vipul S","rank":"1","accuracy":"80"}]}
and I am getting these values into my android app very easily using volley library but the thing is I need something to make that email variable dynamic because I am getting user email from users phone so I need to pass a variable to PHP through my android, so is there any chance that I can pass this through my URL which is written in android
my URL is: "http://thehostels.in/judgement_files/myleaderboard.php"
so can I pass the emails from URL that will be taken by PHP file through
$email=$_GET['userEmail'];
finally what I want is to that the JSON should change according to email that changes due to URL, I hope I made sense

You can pass email id in URL as
http://thehostels.in/judgement_files/myleaderboard.php?userEmail=test#test.com
and you can use this parameter in PHP as below
$email = $_GET['userEmail'];
$sql = "select * from Leaderboard where email='$email'";
For security reason you should use proper validation for email id.

Related

Grabbing JSON from a activity and using it on a php page

I have a activity on Android Studio, grabbing information from JSON (that came from index.php), now I want to use this information on a new php page, so I can use it on a new query, to insert new values into phpmyadmin.
Here's the code from index.php
<?PHP
include_once("connection.php");
if(isset($_POST['txtEmail']) && isset($_POST['txtPassword'])){
$email = $_POST['txtEmail'];
$password = $_POST['txtPassword'];
$query = "SELECT * FROM tbl_user WHERE email = '$email'
AND password = '$password'";
$result = mysqli_query($conn, $query);
if($result->num_rows > 0){ //has record. correct username and password
while($row[] = $result->fetch_assoc())
{
$json = json_encode($row);
echo $json;
}
} else {
echo "0 results";
exit;
}
exit;
}
?>
Wich will display the JSON, what I want is to use one of the JSON fields (for example the ID returned) in a new query in another php page, for example in the WHERE statement.
To use the JSON on Android Studio and move it between activities I do the following:
JSONArray jArray = new JSONArray(s);
JSONObject json_data=null;
json_data = jArray.getJSONObject(0);
ct_name = json_data.getString("name");
ct_address = json_data.getString("address");
ct_email = json_data.getString("email");
ct_phone = json_data.getString("phone");
Bundle b = new Bundle();
b.putString("userdata",json_data.toString());
intent.putExtras(b);
startActivity(intent);
Another idea that I had was if I could grab the ID (field on the database), on the first php page (Index.php) and then use it on the second one, so it knows wich user is logged in at that time. I tried using Sessions to do this, but with no luck. Maybe because the php pages don't interact with eachother directly?
Phpmyadmin is a front end for MySQL. Do you mean you want to insert your data into a MySQL database table? If so, you will need to have a database name as well as a username and password. Then you will have to use mysqli or PDO to interact with the database.

Make a json object properly by retrieving data by MYSQL

I am making a php project and I am currently hosted it in my local xammp server.
I need to connect this to an android application using volley library.
So I need to make a json object.In orders I have stored picture of the product and orders.php page that I retrieve it via orderno.
In orders.php page I can see all my orders with images.
When trying to make the json object I need get this image url.I try to get it via order no but all the times i can't get the correct url.
Can anyone tell me how to solve this?
This is for making that json object.
This is the link to the image current json which I got
I want to make my image URL like below image:
There were multiple issues with your PHP code. However, I tried the resolve most of them. Still if you see any issues let me know, I will try to help you. I will highlight the changes I did to the code, for you to understand.
Here goes the PHP code to retrieve values:
<?php
// array for JSON response
$response = array();
include("config.php");
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD) or die(mysqli_error());
$db = mysqli_select_db($con,DB_DATABASE) or die(mysqli_error());
$result = mysqli_query($con,"SELECT * FROM ORDERS");
if (!empty($result)) {
// check for empty result
if (mysqli_num_rows($result) > 0) {
// user node
$response["orderdata"] = array();
while($row = mysqli_fetch_array($result))
{
$orderdata = array();
$i = $row["ITEM_ID"];
$url = "http://localhost/Online_shopping/admin/'$i'.jpg";
$orderdata["url"] = $url;
$orderdata["item_id"] = $row["ITEM_ID"];
$orderdata["price"] = $row["PRICE"];
$orderdata["size"] = $row["SIZE"];
array_push($response["orderdata"], $orderdata);
}
// success
$response["response_code"] = 200;
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["response_code"] = 999;
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["response_code"] = 999;
// echo no users JSON
echo json_encode($response);
}
?>
You need to first setup a connection with your database which is located in a particular server.
Using that connection, you need to query in your table to get the data.
mysql_query, mysql_fetch_array is deprecated. You have to use mysqli instead of mysql.
Lastly, you can go through this post to learn simple basic operations using PHP.

retrieving data from a mysql database

I have a sign up page that has input boxes where you would enter your name, email address and password. After submitting that form there is a sign in page where it checks to see if you are in the database by SELECT * FROM users WHERE name = '$_POST[name]' AND email = '[email]'. This all works fine, but when you actually get into the site on your account i want to have a message at the top that says 'Welcome back (name from database). To do this i used $name = mysql_query("SELECT first_name FROM users WHERE email = $email"); and had <?php echo $name ?> at the top. This won't work though. Why?
"mysql_query" has been deprecated, you want to use "mysqli_query" now. See here: http://php.net/manual/en/function.mysql-query.php
To answer your question, performing the query creates an array, you need to followup on that by fetching from the array.
If you are doing this in a procedural style then the final code should look something like this:
$link = mysqli_connect("localhost", "my_user", "my_password"); // connect to the database
$query="SELECT first_name FROM users WHERE email = '".$email."'"; // create the query. Note the quotes arounds the email variable.
$result=mysqli_query($link, $query); // run the query
$row=mysqli_fetch_array($result); // fetch the array that is returned from the query
$name=$row['first_name']; // assign the first_name field to the $name variable
echo "Hello ".$name.","; // output the variable

Android: Mysql, php, json, http and security

I'm quite new to the Android and Java world; I'm trying to develop an app that need to get and insert information in a database.
I searched on the net, and I found that the best solution is to use HTTP connection using PHP files with Jsons outputs as an interface between the application and the Mysql Db.
However, I can't figure out how to 'secure' everything.
Example:
To change the nickname of an user i connect my app to a php page with this code:
$nickname = $_POST['nickname'];
$id = $_POST['id'];
nicknameChange($id,$nickname);
function nicknameChange($id,$nickname){
global $mysqli;
$sql = "UPDATE users SET nickname = ? WHERE id = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('si',$nickname,$id);
$stmt->execute();
}
Via post i pass the id of the user that wanna change nickname and the new nickname, how can i be sure that no one will 'manipulate' that page passing to it ids of other users to change their nickname?
I read on the Net that an HTTPS connection may solve the problem, is that enough?
In your client (android) and in server(php) you need use a key like 'API KEY'
In server
$message = 'Access denied';
$checkApi = false;
if(function_exists('apache_request_headers')){
if(apache_request_headers()['Authorization'] == '2d30ff242f8650954bfe8c993f084f4f')
$checkApi = true;
}
or
if(isset($_SERVER["Authorization"]) && $_SERVER["Authorization"] == '2d30ff242f8650954bfe8c993f084f4f'){
$checkApi = true;
}
Client: in your request you will put string '2d30ff242f8650954bfe8c993f084f4f' with key 'Authorization' to Header request.
This is a private key!

Retrieving information on a certain user PHP MySQL

I have created a booking system which uses a clients username from their log in to auto populate a user name field when making a booking. I am not sure of how to get other information like their full name and ID from the database into these fields. Below is the code I have used to verify log in and store their username:
<?php
// Start up your PHP Session
session_start();
// If the user is not logged in send him/her to the login form
if ($_SESSION["Login"] != "YES_client") {
header("Location: login.php");
}
$username = $_SESSION["username"];
?>
I have also implemented the user name in the field using the following code:
echo "<input type='text' name='name' class='form-control' id='FullInputName' value=" . $username . ">"
Is there something simple I am missing? I have tried various methods to display the full data like using $row["Client_ID"] etc but could not get this to work for only the client who is logged into the system. My SQL statement is as follows:
"SELECT * FROM client WHERE Client_username= $username"
I would like to use the Client_ID in the select statement also to make it Unique. I have tried but got various errors.
Any help would be much appreciated!
EDIT
This is the code I have now tried to implement:
$query = "SELECT * FROM client WHERE Client_username='$username'";
echo $query;
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo $row['Client_username'];
}
But it is not working correctly - I am receiving this error:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
Starting with your query i think is not correct.
If you are selecting a row and the type is a VARCHAR you need to add single quotes like this:
"SELECT * FROM client WHERE Client_username= '$username'"
Later you can read the results like
(pseudocode) while($row = mysqli_fetch_array) $row['Client_username']
something like that.
Tell me if this works for you

Categories