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

我在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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?