0

Here is the php script I'm using for registration of new users

PHP

<?php
require "init.php";

$name      = $_POST["user"];
$user_name = $_POST["user_name"];
$user_pass = $_POST["user_pass"];

$sql_query = "insert into user_info values('$name','$user_name','$user_pass');";


?>

Android Log

06-10 19:55:40.395 4694-4838/com.example.clint.mysqltesting W/System.err: java.io.FileNotFoundException: http://192.168.0.103/webapp/register.php

My Background Task BackgroundTask.java

package com.example.clint.mysqltesting;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
* Created by CLINT on 6/11/2016.
*/
public class BackgroundTask extends AsyncTask<String, Void, String> {

    Context ctx;

    BackgroundTask(Context ctx){

        this.ctx = ctx;


    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        String reg_url = "http://192.168.0.103/webapp/register.php";
        String login_url = "http://192.168.0.103/webapp/login.php";
        String method = params[0];
        if(method.equals("register")){

            String name = params[1];
            String user_name = params[2];
            String user_pass = params[3];

            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("user", "UTF-8")+ "=" +URLEncoder.encode(name, "UTF-8")+ "&" +
                        URLEncoder.encode("user_name", "UTF-8")+ "=" +URLEncoder.encode(user_name, "UTF-8")+ "&"
                        +URLEncoder.encode("user_pass", "UTF-8")+ "=" +URLEncoder.encode(user_pass, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                return "registration Success";


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
    }

}

What is the reason that I am getting an Undefined Index? I have seen some questions related to this that are saying use the (isset) can resolve this trouble. I have tried doing this, but still not able to add the data to the database from the Android app.

buczek
  • 2,011
  • 7
  • 29
  • 40
Clint Paul
  • 313
  • 2
  • 6
  • 18
  • I don't know Android code, but if you have your entire code in the same page, then use `isset()` or `!empty()` against your POST arrays. Btw, I don't see you executing the query, unless you left that out. and by looking that the .log; seems to me that the file can't be found. Check your path(s). Use error reporting for the PHP http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Jun 10 '16 at 14:56
  • Your android log says, there's a `FileNotFoundException`. Is the testing device and server connected to the same network? – Prerak Sola Jun 10 '16 at 14:58
  • could you please change my php code as the way you proposed ? and what you mean by executing the query ? @Fred-ii- – Clint Paul Jun 10 '16 at 15:02
  • @PrerakSola Yes . My testing device and sever is connected to the same network . – Clint Paul Jun 10 '16 at 15:03
  • `$sql_query = "insert...` I don't see how you're using `mysql_query()` or `mysqli_query()` or the PDO equivalent of executing the query (the MySQL API to connect with, is unknown). For the conditionals: i.e.: `if(!empty($_POST['x'])){...}` and add to it with `&&` and other arrays. – Funk Forty Niner Jun 10 '16 at 15:04
  • Okay, so now can you access the php pages from your phone's browser? – Prerak Sola Jun 10 '16 at 15:04
  • @PrerakSola sorry , I don't understand – Clint Paul Jun 10 '16 at 15:08
  • Go to `http://192.168.0.103/webapp/register.php` from your testing evice's browser – Prerak Sola Jun 10 '16 at 15:10
  • @PrerakSola it says "you don't have permission to access /webapp/register.php on this server. Apache/2.4.17 (Win64) PHP/5.6.16 Server at 192.168.0.103 Port 80 – Clint Paul Jun 10 '16 at 15:13
  • It means, your app is not able to communicate with the server. Server is blocking the request. Check this question, it might help you: http://stackoverflow.com/questions/10873295/error-message-forbidden-you-dont-have-permission-to-access-on-this-server – Prerak Sola Jun 10 '16 at 15:15

0 Answers0