Data isn't sending from AndroidStudio to phpMyAdmin / 000webhost - php

I'm trying to create a basic Register and Log in Application by following this tutorial: https://www.youtube.com/watch?v=JQXfIidfFMo&t=193s and because the code in Android Studio has shown 0 Compile Errors I've decided not to put that here.
What's wrong
Many users have reported in the comments section both old and new that they could not get their information sent to their database so any help would be greatly appreciated. The user inputs their 'name', 'username', 'password', and 'age' to register and this information is to then sent to my database hosted on 000webhost. Everything has been followed to the T.
The name of the table is called 'user'.
Here is the code to my Login.php file
<?php
$con = mysqli_connect("localhost:3306","id7390457_user","abcd1234","id7390457_data");
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ssis", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $user_id, $name, $age, $username, $password);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["name"] = $name;
$response["age"] = $age;
$response["username"] = $username;
$response["password"] = $password;
}
echo json_encode($response);
?>
Here is the code to my Register.php file
<?php
$con = mysqli_connect("localhost", "id7390457_user", "abcd1234", "id7390457_data");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (name, age, username, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "ssis", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
I have been under the impression that the line
$con = mysqli_connect("localhost", "id7390457_user", "abcd1234", "id7390457_data");
Is followed by using ('hostname', 'username', 'password', 'nameofdatabase')
However as I am doing this from my Mac so my host is 'localhost'
My DB Name is: id7390457_data
My DB User is: id7390457_user
I've tried using 'localhost:3306'
I've tried just 'user' and 'data'
I've watched YouTube videos, visited websites in other languages and none have seemed to provide any advise.
I've deleted multiple databases starting from scratch but I still face the issue whereby my information isn't being sent to phpMyAdmin on 000webhost.
My webhost doesn't have an index.html page but I don't think that this would be the issue.
Thank you
Any help would mean the world to me. Thank you wish you well!

Hi and welcome to StackOverflow!
You are right, the mysqli_connect expects the host, user, pass and db name in that order, so if the database is running and the values are correct as you said, this looks OK.
However on this line, note the second argument ssis:
mysqli_stmt_bind_param($statement, "ssis", $name, $age, $username, $password);
It states the type of the parameters, s for string and i for integer. It looks like the order is messed up, because age is expected as a string and username as an integer. If you replace it with siss, I would expect it to work.
More info: https://secure.php.net/manual/en/mysqli-stmt.bind-param.php

Related

Trouble connecting to my database and inserting info via PHP script

This script works through a free webhosting service, but not the paid hosting service I have via iFastNet. I use cpanel to create databases and etc.
Here's my script for registering new users in my database. It is communicating with files in my android studio project.
<?php
$con = mysqli_connect("localhost", "my_username", "my_password", "userdatabase_name");
$email = $_POST["email"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM users WHERE email = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $id, $username, $email, $password);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["username"] = $email;
$response["password"] = $password;
}
echo json_encode($response);
?>
Not sure if any of you have experience with this. I'm assuming it's the web hosting service. Hopefully one of you have had this problem, and can provide a simple solution.
It's important to bring up that I can connect to the database via MYSQL Workbench, and I'm able to insert data into my table there. I just can't insert data via the simple user registration I created for an android application. It worked via a free web hosting service though.
-Also, before anyone mentions it, my user has all privileges for the database on the paid web hosting service.

Connecting an Android app to a database

I'm trying to get my Android Studio register app to send information to the database. I'm using a WAMP server. Below is my Register.php code that was uploaded and if anyone sees anything wrong with it could you help to amend it?
<?php
$con = mysqli_connect("localhost", "root", "", "users");
$Name = $_POST["Name"];
$DoctorNumber = $_POST["DoctorNumber"];
$Password = $_POST["Password"];
$statement = mysqli_prepare($con, "INSERT INTO users(Name, DoctorNumber, Password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $Name, $DoctorNumber, $Password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
You can't connect your Android (or even iOS) app directly to a MysQL database. You'll have to create a webapp that exposes it, and the use a library like Retrofit to make the http calls.

Response from database is not received to php file

I have been trying to build a register and login android app using php and mysql with android studio. It appears that the values are being entered to the databse but it is not giving a response back. Table name is harry and php file is:
<?php
define('DBUSER', 'id650955_gokulm100');
define('DBPASS', 'gokulm100');
define('DBHOST', 'localhost');
define('DBNAME', 'id650955_harry');
$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if (!$conn) {
die('error connecting to database');
}
echo 'you have created case';
if(isset($_POST["username"]) && isset($_POST["email"]) && isset($_POST["phone"]) && isset($_POST["password"]))
{
$username = $_POST["username"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$password = $_POST["password"];
$statement = mysqli_prepare($conn, "INSERT INTO harry (username, email, phone, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "ssis", $username, $email, $phone, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = 1;
echo json_encode($response);
}
else{
echo "not set";
}
?>
assuming that the data is inserted to the database you need to look at your output, it looks like you're sending json text via json_encode and in the same time echo a string "you have created case"
so your output will be a mix of simple characters and some json string. which probably send a parsing error to the callback in the android side of the code.
first you should add a header that indicates the you will be returning json code at the top of the file
header("Content-Type: application/json",true);
then change all your output to be json , any
echo "str";
should be
echo json_encode(array("message"=>"str"));
then see if you still don't get any message back.

PHP files fail to connect to MySQL Android Studio login/register app

I am creating an app that will allow users to login and register. The app appears to be running correctly however there issue with the connection between the app and the server. When I run the app and register a new user, the details I just enter do not enable me to login. I think the problem lies with the PHP files as it does not appear as though registered users are being added to the SQL database, however I cannot see how to fix it. When I open the file in Chrome I get the PHP error message:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'a9650904_John'#'10.1.1.27' (using password: YES) in /home/a9650904/public_html/Register.php on line 2
These are my PHP files
FetchUserData.php:
<?php
$con=mysqli_connect("mysql7.000webhost.com","a9650904_John","a9650904_********","a9650904_User");
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $county, $username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user["county"] = $county;
$user["username"] = $username;
$user["password"] = $password;
}
echo json_encode($user);
mysqli_close($con);
?>
Register.php
<?php
$con=mysqli_connect("mysql7.000webhost.com","a9650904_John","a9650904_********","a9650904_User");
$name = $_POST["county"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO User (county, username, password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $county, $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
Here are links to the files
http://johngriffin.netau.net/FetchUserData.php and http://johngriffin.netau.net/Register.php
If anybody has any idea as to what I could do to fix this problem, your help would be very much appreciated.
This error
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'a9650904_John'#'10.1.1.27' (using password: YES) in /home/a9650904/public_html/Register.php on line 2
signifies that your php file cannot connect to your database because of error in your database configuration access information.
Check that you have add correct information here
$con=mysqli_connect("", "", "", "")

How to connect or link to localhost server address in Android Studio

I am following a tutorial of making a login register for android by tonakami TV in youtube, I encountered a problem where the android app can't update or connect to the database via LOCALHOST XAMPP. For one week I'm stuck searching all possible solution but found none. Perhaps you guys can help me with the problem of my PHP files(register.php and fetchuserdata.php), I also found out that I also have this error in the php files: mysqli_connect(): (HY000/1044): Access denied for user 'my_user'#'localhost' to database 'my_db'. I grant all privileges but still can't connect and I don't know if the problem is from the PHP files, the link to the server in android studio or of XAMPP localhost phpmyadmin. Thanks !
Screenshot of Android Studio: highlighted link
Register.php
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO User (name, age, username, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
FetchUserData.php
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user["name"] = $name;
$user["age"] = $age;
$user["username"] = $username;
$user["password"] = $password;
}
echo json_encode($user);
mysqli_close($con);
?>
Instead of localhost use this: http://10.0.2.2
Reference

Categories