0

I am sending the 3 integer values from Java to PHP in order to either change or add entries in my database, depending if an entry already exists. I am having trouble with binding the variables and get the following error:

Notice: Undefined index: userid in C:\xampp\htdocs\attendanceradio.php on line 9

Notice: Undefined index: eventid in C:\xampp\htdocs\attendanceradio.php on line 10

Notice: Undefined index: attending in C:\xampp\htdocs\attendanceradio.php on line 11

Notice: Undefined index: attending in C:\xampp\htdocs\attendanceradio.php on line 12

Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\xampp\htdocs\attendanceradio.php on line 23

In this question I am specifically asking about the Warning that I get, I am new to this and would like to know what I am doing wrong. Any help would be amazing, thanks in advance!

Visual:enter image description here Here is my code:

<?php

    $user = 'root';
    $pass = '';
    $db = 'testuser';

    $con=mysqli_connect('localhost', $user, $pass, $db) or die('Unable to connect');

    $userid = $_POST['userid'];
    $eventid = $_POST['eventid'];
    $attending = $_POST['attending'];
    $attending2 = $_POST['attending'];

    $statement = mysqli_prepare($con, 
    'INSERT INTO user_has_event(user_user_id, event_event_id, attendance)
        VALUES (?, ?, ?)
        ON DUPLICATE KEY UPDATE attendance = ?');

    mysqli_stmt_bind_param($statement, 'iiii', $userid, $eventid, $attending, $attending2);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $userid, $eventid, $attending, $attending2);

    mysqli_stmt_close($statement);

    mysqli_close($con);
?>

I have also tried:

 $statement = mysqli_prepare($con, 
'INSERT INTO user_has_event(user_user_id, event_event_id, attendance)
    VALUES (?, ?, ?)
    ON DUPLICATE KEY UPDATE attendance = ?');

mysqli_stmt_bind_param($statement, 'iii', $userid, $eventid, $attending);

Java code:

public class ServerRequests {

    ProgressDialog progressDialog;
    public static final int CONNECTION_TIMEOUT = 1000 * 15;
    public static final String SERVER_ADDRESS = "http://10.0.2.2:80/";//Connects to the emulator

    public ServerRequests(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setTitle("Processing");
        progressDialog.setMessage("Please wait...");
    }

    public void GetAttendanceDataAsyncTask(Attendance attendance, GetAttendanceCallback attendanceCallBack) {
        progressDialog.show();
        new getAttendanceDataAsyncTask(attendance, attendanceCallBack).execute();
    }    
public class getAttendanceDataAsyncTask extends AsyncTask<Void, Void, Void> {
        Attendance attendance;
        GetAttendanceCallback attendanceCallBack;

        public getAttendanceDataAsyncTask(Attendance attendance, GetAttendanceCallback attendanceCallBack) {
            this.attendance = attendance;
            this.attendanceCallBack = attendanceCallBack;
        }

        @Override
        protected Void doInBackground(Void... params) {
            Map<String, Integer> dataToSend = new HashMap<>();
            dataToSend.put("userid", MainActivity.getUserID());
            dataToSend.put("eventid", EventPage.getEventID());
            dataToSend.put("attending", EventPage.getRadio());

            String encodedStr = StrEncoder.getEncodedData(dataToSend);

            BufferedReader reader = null;

            try {
                URL url = new URL(SERVER_ADDRESS + "/attendanceradio.php");

                HttpURLConnection con = (HttpURLConnection) url.openConnection(); //Opens attendanceradio.php

                con.setConnectTimeout(CONNECTION_TIMEOUT); //Setting timeouts
                con.setReadTimeout(CONNECTION_TIMEOUT);

                con.setRequestMethod("POST"); //Request post

                con.setDoOutput(true);
                OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());

                writer.write(encodedStr);
                writer.flush();


                StringBuilder sb = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String line;
                while ((line = reader.readLine()) != null) { //Read until there is something available
                    sb.append(line + "\n");     //Read and save line by line
                }
                line = sb.toString();           //Saving complete data received in string

                //Check values received in Logcat
                Log.i("custom_check", "The values received in the store part are as follows:");
                Log.i("custom_check", line);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();     //Closing the
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }}
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
kmil
  • 243
  • 3
  • 13
  • possible duplicate of [PHP: Notice: Undefined variable and Notice: Undefined index](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner Mar 27 '16 at 17:24
  • ^ your HTML form which is unknown/unshown, failed you ^ – Funk Forty Niner Mar 27 '16 at 17:26
  • And of course the usual: you should use PDO objects rather than the procedural mysqli statements. – Gralgrathor Mar 27 '16 at 17:29
  • @Fred-ii- Not sure how that's a duplicate, but whatever you say.. – kmil Mar 27 '16 at 17:30
  • *You think?* => `Notice: Undefined index` how many times? Start by fixing those and your binds will follow. – Funk Forty Niner Mar 27 '16 at 17:31
  • @Fred-ii- Isn't it that because I pull the variables from Java? Like I said, I'm new to PHP. – kmil Mar 27 '16 at 17:33
  • TBH, I know nothing about JAVA but seeing what you have now seems *kind of* OK. That guy posted an answer, he should be helping you out with this, rather than just telling you (and what should have been a comment really) that you've not sent 3 integer values. I don't see it as an "answer" per se. Wouldn't you agree? – Funk Forty Niner Mar 27 '16 at 17:34
  • If those are not integers but strings, then change all your `i`'s to `s` and make sure the column types match what you're trying to send. Plus, are you not using a form with this? – Funk Forty Niner Mar 27 '16 at 17:35
  • As of now, the .PHP file is stored in xampp and called from Java. – kmil Mar 27 '16 at 17:40

2 Answers2

0
Notice: Undefined index: userid in C:\xampp\htdocs\attendanceradio.php on line 9

tells you that you in fact have not sent 3 integer values from your Java applet to your PHP script.

I suspect your Java application encodes your data as GET parameters. Try $_GET or $_REQUEST instead of $_POST. Dump $_REQUEST to see what gets sent and how.

Gralgrathor
  • 216
  • 1
  • 8
  • Isn't that because I pull the variables from Java? – kmil Mar 27 '16 at 18:44
  • I've amended my answer. You probably send the parameters as GET data, not POST. – Gralgrathor Mar 27 '16 at 20:12
  • Thanks for taking the time to look at this. I'll check it out now although I'm pretty sure I send them as posts. Check my JAVA : `con.setRequestMethod("POST");` – kmil Mar 27 '16 at 20:17
  • No good:( I think the thing that is holding me back now is the part where I assign the variables. `{"attending":0,"eventid":1,"userid":1}` is received inside the PHP form when i `print_r($_POST);`, I feel like there is a problem pulling the values from there. – kmil Mar 27 '16 at 20:24
  • Please post what var_dump($_POST) *literally* say. – Gralgrathor Mar 27 '16 at 23:22
  • O, and I'm also curious to know how you declared and instantiated StrEncoder. – Gralgrathor Mar 27 '16 at 23:25
0
$data = json_decode(trim(key($_POST), '[]'), true);
    $userid = $data['userid'];
    $eventid = $data['eventid'];
    $attending = $data['attending'];

This fixed my problem, the variables were not being assigned properly. Had to pull them out of the array. Also removed the result bind.

kmil
  • 243
  • 3
  • 13