我正在将3个整数值从Java发送到PHP,以便更改或添加数据库中的条目,具体取决于条目是否已存在。 我在绑定变量时遇到问题并收到以下错误:
<?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);
?>
在这个问题中,我特别询问我得到的警告,我是新手,想知道我做错了什么。任何帮助都会很棒,提前谢谢!
$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);
我也尝试过:
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;
}}
Java代码:
<script>
function touchTest() {
document.getElementById('touch').addEventListener('touchstart', function () { alert ('touch'); } , false);
document.getElementById('click').addEventListener('click', function () { alert('click'); } , false);
}
document.addEventListener('DOMContentLoaded', touchTest);
</script>
答案 0 :(得分:0)
Notice: Undefined index: userid in C:\xampp\htdocs\attendanceradio.php on line 9
告诉您实际上 不 从您的Java小程序向您的PHP脚本发送了3个整数值。
我怀疑您的Java应用程序将您的数据编码为GET参数。尝试$ _GET或$ _REQUEST而不是$ _POST。转储$ _REQUEST以查看发送的内容和方式。
答案 1 :(得分:0)
$data = json_decode(trim(key($_POST), '[]'), true);
$userid = $data['userid'];
$eventid = $data['eventid'];
$attending = $data['attending'];
这解决了我的问题,变量未正确分配。不得不将它们从阵列中拉出来。还删除了结果绑定。