我正在做一个小学校项目 我需要在数据库上上传图片 我只是不知道如何在上传图片时添加用户ID条件 因为我想以特定的用户ID发送图像,所以该ID已连接到应用程序。
public static final String UPLOAD_URL = "urlto/upload.php";
public static final String UPLOAD_KEY = "image";
private void uploadImage() {
class UploadImage extends AsyncTask<Bitmap,Void,String> {
ProgressDialog loading;
RequestHandler rh = new RequestHandler();
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(UserProfilActivity.this, "Uploading Image", "Please wait...",true,true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Bitmap... params) {
Bitmap bitmap = params[0];
String uploadImage = getStringImage(bitmap);
HashMap<String,String> data = new HashMap<>();
data.put(UPLOAD_KEY, uploadImage);
String result = rh.sendPostRequest(UPLOAD_URL,data);
return result;
}
}
UploadImage ui = new UploadImage();
ui.execute(bitmap);
}
PHP代码
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_POST['image'];
require_once('dbConnect.php');
$sql = "INSERT INTO users (image) VALUES (?)";
$stmt = mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt,"s",$image);
mysqli_stmt_execute($stmt);
$check = mysqli_stmt_affected_rows($stmt);
if($check == 1){
echo "Image Uploaded Successfully";
}else{
echo "Error Uploading Image";
}
mysqli_close($con);
}else{
echo "Error";
}
应该类似于“更新用户图像=:new_image其中(:user_id = user_id)” 我只是不明白如何将user_id参数发送到php代码,以及在php中进行哪些修改。
谢谢
编辑:
好的,这个php可以正常工作
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_POST['image'];
$user_id = $_POST['user_id'];
require_once('dbConnect.php');
$sql = "Update users SET image=? WHERE user_id=?";
$stmt = mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt,"si",$image,$user_id);
mysqli_stmt_execute($stmt);
$check = mysqli_stmt_affected_rows($stmt);
if($check == 1){
echo "Image Uploaded Successfully";
}else{
echo "Error Uploading Image";
}
mysqli_close($con);
}else{
echo "Error";
}
现在我需要发送用户ID参数的Java代码,我已经使用在线POST工具进行了测试