Response from database is not received to php file - php

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.

Related

form input will not POST to MYSQL but will show on screen

The form sends the data to this page. The print_r outputs everything I want to put into the table onscreen to check it's there, but nothing goes to the table. I have only managed to populate the table manually in phpmyadmin. Iam sorry if it's a really easy fix - I have only been learning for two weeks!
There are no errors showing in the logs or on screen when I run the page. The print_r does echo the array as it should be but nothing appears in the table
<?php
session_start();
// Change this to your connection info.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'users';
$username = ($_POST['username']);
$password = ($_POST['password']);
$companyName = ($_POST['companyName']);
$confirmPassword = ($_POST['confirmPassword']);
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS,
$DATABASE_NAME);
if (mysqli_connect_errno()) {
// If there is an error with the connection, stop the script and
display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
print_r ($_POST);
// Now we check if the data was submitted, isset() function will check
//if the data exists.
if (!isset($_POST['username'], $_POST['password'],
$_POST['companyName'])) {
// Could not get the data that should have been sent.
die ('Please complete the registration form!');
}
// Make sure the submitted registration values are not empty.
if (empty($_POST['username']) || empty($_POST['password']) ||
empty($_POST['companyName'])) {
// One or more values are empty.
die ('Please complete the registration form');
}
print_r ($_POST);
// We need to check if the account with that username exists.
if ($stmt = $con->prepare('SELECT id, password FROM phplogin WHERE
username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the
//password using the PHP password_hash function.
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
// Store the result so we can check if the account exists in the
// database.
if ($stmt->num_rows > 0) {
// Username already exists
echo 'Username exists, please choose another!';
} else {
// Username doesnt exists, insert new account
/* $stmt = $con->prepare('INSERT INTO phplogin (username, password,
companyName ) VALUES (?, ?, ?)');*/
if (false !== true){
/* We do not want to expose passwords in our database, so hash the
password and use password_verify when a user logs in.
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt->bind_param('sss', $_POST['$username'], $password,
$_POST['$companyName']);
$stmt->execute();*/
$sql = 'INSERT INTO phplogin (username, password, companyName )
VALUES ($username, $password, $companyName)';
echo 'You have successfully registered, you can now login!';
echo (" ".$password." ".$username." ".$companyName);
echo ' well done';
} else {
/* Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.*/
echo 'Could not prepare the new statement!';
print_r ($_POST);
}
}
}
$con->close();
?>
//$sql = 'INSERT INTO phplogin (username, password, companyName ) VALUES ($_POST[username], $password, $_POST[companyName])';
PHP thinks it should execute VALUES even though it is not any proper action. Use /* THIS IS COMMENT */ because it prevents stuff like this happening.
Also as a side note: Do not assign values in if statement. You can assign $stmt on its own line and just check
If($stmt === true) {}
Or
If($stmt !== true) {}
You get the point.
Also another side note is that you should prefer using PDO. It is alot of easier to handle and understand because of ts syntax and it makes OOP much much more easier. Mysqli is ok to use, but i personally do not recommend using it.

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

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

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 method doesnt working properly in php script

I make a project in android studio for login/register.
I use volley library and a php script that connects my java code with my database.The POST method working perfectly and i can insert information in my database.But i get nothing on response.Seems there is an error in my php script.
Here is the php script:
$con = mysqli_connect("mysql10.000webhost.com", "a3288368_user", "abcd1234", "a3288368_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, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
Any ideas what is going wrong?
Use
print_r(json_encode($response));
You can't echo json string.
Before echoing out the response, you should set a JSON header:
header("Content-type: application/json; charset=utf-8");
This tells the HTTP client to expect a JSON type of content.

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