doukun8944
doukun8944
2016-03-27 17:23
浏览 82
已采纳

我在SQL中绑定PHP变量时遇到问题

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 + "
");     //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;
        }}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongwen9947
    dongwen9947 2016-03-28 17:40
    已采纳
    $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.

    点赞 评论
  • dpn517111
    dpn517111 2016-03-27 17:27
    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.

    点赞 评论

相关推荐