1

I am trying to connect my android app with database on my local server. It is actually connecting but when its trying to perform insertion into database , I am getting errors:

Notice: Undefined index: user_login in C:\wamp\www\webapp\register.php on line 4
Notice: Undefined index: user_password in C:\wamp\www\webapp\register.php on line 5
Notice: Undefined index: user_phonenumber in C:\wamp\www\webapp\register.php on line 6
Notice: Undefined index: user_email in C:\wamp\www\webapp\register.php on line 7

On my register.php i have

require "init.php";

$user_login = $_POST["user_login"];
$user_password = $_POST["user_password"];
$user_phonenumber = $_POST["user_phonenumber"];
$user_email = $_POST["user_email"];

$sql_query = "insert into user_db values('$user_login','$user_password','$user_phonenumber','$user_email');";

Android code :

URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                Log.d("in background", " connected");
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
                String data = URLEncoder.encode("user_login","UTF-8") + "="+URLEncoder.encode(Loginstr,"UTF-8")+"&"+
                        URLEncoder.encode("user_password","UTF-8") + "="+URLEncoder.encode(Passwordstr,"UTF-8")+"&"+
                        URLEncoder.encode("user_phonenumber","UTF-8") + "="+URLEncoder.encode(Numberstr,"UTF-8")+"&"+
                        URLEncoder.encode("user_email","UTF-8") + "="+URLEncoder.encode( Emailstr,"UTF-8");
                        bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();

I would also ask how to make one thing. In my database I have created to be filled by new user and one field created to hold unique id for user. My question is: After creation of new user , will this id field be filed with new uniqe id corelated with user? (AUTO_INCREMENT option is marked). If not , what should i do to make such a thing.

I am really new with SQL and PHP so i hope I have explained everything correct. Thanks in advance

Mykola
  • 3,343
  • 6
  • 23
  • 39
user2999425
  • 125
  • 1
  • 11
  • [This](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) explains the error. I would guess the POST part of your HTTP request is being set improperly. – HPierce Nov 20 '15 at 22:25

1 Answers1

0

Firstly, you are not getting 'errors', but you are getting 'notices'.

You can disable displaying of notices with the following line:

ini_set('error_reporting', E_ALL ^ E_NOTICE);

For development use it is good to set the error reporting to display the errors 'on the page':

ini_set('display_errors', true);

But for production this should be set to false, and errors logged somewhere else.

Secondly, if your MySQL table has a primary key with an AUTO_INCREMENT then your insert will automatically generate an Id. You can access this with the LAST_INSERT_ID() MySQL function (or the PHP equivalent, depending on wether you are using mysqli or PDO).

Andrew McCombe
  • 1,603
  • 1
  • 12
  • 13