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.
Related
NEED HELP
We are creating a Cross platform application in xamarin (Android / IOS). We have made a login system in our app by passing everytime when user is logged in, the Id of the user from the database. This Id is send to other pages in our app so that we could use this Id to access the other information (in other pages in our app) of that specific user with the specific Id.
Example:
We passed the Id to the profile page of the user that is logged in. As you can see in the screen beneath we want to get the username of that user so that we could use it in our app.
When we pass the Id using a HTML page we get the correct data from the database as you can see. It is this content we want to show instead of "Username" (frist picture).
The php-script we used to get this data out of our database (don't worry about mysql-injection the app is jsut for us we will change this later):
<?php
$db = new mysqli('localhost', 'root', 'Antjim0809!','sendmeadrink_official');
$Id = mysqli_real_escape_string($db, $_POST['Id']);
$res = mysqli_query($db, "SELECT Username from User WHERE Id = '$Id'");
while ($row = $res->fetch_assoc())
{
echo $row['Username'];
}
mysqli_close($db);
?>
But when we try to get this information using Xamarin we get the 'null' instead of 'Lucino23' as you can see in the output:
The Xamarin code of our Profile page looks like this (ProfilePage.XAML):
<Label Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="3"
Text="{Binding Username}"
FontAttributes="Bold"
FontSize="30"/>
The code behind (ProfilePage.XAML.cs)
readonly HttpClient client = new HttpClient(new HttpClientHandler());
readonly user u = new user() { Id = string.Empty, Username = string.Empty, Email = string.Empty, Passwd = string.Empty, Age = string.Empty };
public async void GetUserData(string Id)
{
HttpResponseMessage res;
u.Id = Id;
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("Id", u.Id)
});
res = await client.PostAsync("http://10.0.2.2/*/*/*/UsernameById.php", content);
u.Username = await res.Content.ReadAsStringAsync();
Console.WriteLine("-----------------------------------------");
Console.WriteLine(u.Username);
Console.WriteLine("-----------------------------------------");
}
I resume: When we apply all the code above, the 'Username' (Picture 1) is empty and we cannot see the Username that tis stored in the database. So we want to replace the 'Username' by the string value in our database of a specific user that is logged in.
Thanks to all!
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.
I am developing an adroid app and I faced some trouble about php web service.
I want to get user type information form database and according to the answer I will do some process in the background.
So in my authentication code there is a area like this to get usertype;
function getUserType(){
$sql = "SELECT `usertype` FROM `login_test` WHERE username = '". $this->username2."'
AND password = '".$this->password2."'";
$result = mysqli_query($this->DB_CONNECTION, $sql);
if(mysqli_num_rows($result)>0){
return (?);
}
}
and my in my login code the message will be send here;
if ($userStatus) {
// user existed
// So log him to main page
$json['success'] = 1;
$json['message'] = 'Access Granted';
$json['usertype'] = 'Client';
echo json_encode($json);
Here I dont know how to access a certain field called 'usertype' in my table (I am really new in php) and how to return the value that I got.
Any help will be apreciated
P.S = $userStatus returns ture.
You could try doing this:
$sql = "SELECT * FROM `login_test` WHERE username = '$this->username2' AND password = '$this->password2'";
$result = mysqli_query($this->DB_CONNECTION, $sql);
return $result->fetch_object()->userType;
While please do keep in mind to use prepared statements.
I'm trying to build a relatively simple PHP login script to connect to MySQL database running on my home server. I know the connection works as I've gotten some data returned as I would expect. However, I am having trouble getting the full script to work.
Essentially, I'm taking in a username/password from the user, and I first do a lookup to get the user_id from the users table. I then want to use that user_id value to do a comparison from user_pswd table (i'm storing usernames and passwords in separate database tables). At one point, I was able to echo the correct user_id based on the username input. But I haven't been able to get all the issues worked out, as I'm pretty new to PHP and don't really know where to see errors since I load this onto my server from a remote desktop. Can anyone offer some advice/corrections to my code?
The end result is I want to send the user to another page, but the echo "test" is just to see if I can get this much working. Thanks so much for the help!
<?php
ob_start();
$con = new mysqli("localhost","username","password","database");
// check connection
if (mysqli_connect_errno()) {
trigger_error('Database connection failed: ' . $con->connect_error, E_USER_ERROR);
}
$users_name = $_POST['user'];
$users_pass = $_POST['pass'];
$user_esc = $con->real_escape_string($users_name);
$pass_esc = $con->real_escape_string($users_pass);
$query1 = "SELECT user_id FROM users WHERE username = ?;";
if ($result1 = $con->prepare($query1)) {
$result1->bind_param("s",$user_esc);
$result1->execute();
$result1->bind_result($userid);
$result1->fetch();
$query2 = "SELECT user_pswd_id FROM user_pswd WHERE active = 1 AND user_id = ? AND user_pswd = ?;";
if ($result2 = $con->prepare($query2)) {
$result2->bind_param("is",$userid,$pass_esc);
$result2->execute();
$result2->bind_result($userpswd);
$result2->fetch();
echo "test", $userpswd;
$result2->free_result();
$result2->close();
} else {
echo "failed password";
}
$result1->free_result();
$result1->close();
}
$con->close();
ob_end_clean();
?>
I am developing a game(c#) with database(mysql) and web service(php) to retrieving the data. The issue is the data management. There is a table on database with the name of items and it has some columns like id, item_name, item_description, item_prop, update_date, ownerId. I added 70 items to this table manually. The users can also add some items to this table or they can update the items they have already added in the game. My purpose is retrieving the whole affected rows of the table when the user is first logged in and save it as a json file in the game folder. After, read that file to fill the game environment with those items.
I try a way to achieve this. Firstly, i hold an updateDate variable in the game which is past like "1990-05-10 21:15:43". Second, i send this variable to the webservice as '$lastUpdateDate'; and make a query according to that variable at the database. select * from channels where update_date >= '$lastUpdateDate'; and write these rows in a json file as items.json. after that make a second query to retrieve the time and refresh the updateDate variable in the game. select select now() as 'Result';. In this way user would not have to get the whole table and write in json file every login process. So, it would be good for the performance and the internet usage. The problem occurs when the users update an item which is already added before. I can see the updated item, too with the first query, but I wrote it in json file twice in this way.
php code part of the getItems of my loginuser.php :
<?php
include './function.php';
// CONNECTIONS =========================================================
$host = "localhost"; //put your host here
$user = "root"; //in general is root
$password = ""; //use your password here
$dbname = "yourDataBaseName"; //your database
$phpHash = "yourHashCode"; // same code in here as in your Unity game
mysql_connect($host, $user, $password) or die("Cant connect into database");
mysql_select_db($dbname)or die("Cant connect into database");
$op = anti_injection_register($_REQUEST["op"]);
$unityHash = anti_injection_register($_REQUEST["myform_hash"]);
if ($unityHash == $phpHash)
{
if($op == "getItems")
{
$lastUpdateDate = anti_injection_login($_POST["updateDate"]);
if(!$lastUpdateDate)
{
echo "Empty";
}
else
{
$q = "select * from items where update_date >= '$lastUpdateDate';";
$rs = mysql_query($q);
if (!$rs)
{
echo "Could not successfully run query ($q) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($rs) == 0)
{
echo "No rows found, nothing to print so am exiting";
exit;
}
$arr = array();
while ($row = mysql_fetch_row($rs))
{
$arr = $row;
}
echo json_encode($arr);
}
mysql_close();
}
}
?>
So, how can i solve this problem? Or do you have any better idea for this approach. Help would be much appreciated. Thank you for your time.