How to troubleshoot undefined index error when trying to upload image to server? I'm trying to create an API to upload an image to server using PHP and MySQL
An error:
Notice: Undefined index: img_logo in /opt/lampp/htdocs/Carekkerje/upload.php on line 6 {"success":0,"message":"Error Upload image"}
Error in this section:
$img_logo = $_POST['img_logo'];
My code:
upload.php
<?php
include_once "koneksi.php";
class emp{}
$img_logo = $_POST['img_logo'];
$random = random_word(20);
$path = "images/".$random.".png";
// sesuiakan ip address laptop/pc atau URL server
$actualpath = "http://192.168.43.193/Carekkerje/$path";
$query = mysqli_query($con, "INSERT INTO transaksi (img_logo) VALUES ('$actualpath')");
if ($query){
file_put_contents($path,base64_decode($img_logo));
$response = new emp();
$response->success = 1;
$response->message = "Successfully Uploaded";
die(json_encode($response));
} else{
$response = new emp();
$response->success = 0;
$response->message = "Error Upload image";
die(json_encode($response));
}
// fungsi random string pada gambar untuk menghindari nama file yang sama
function random_word($id = 20){
$pool = '1234567890abcdefghijkmnpqrstuvwxyz';
$word = '';
for ($i = 0; $i < $id; $i++){
$word .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
return $word;
}
mysqli_close($con);
?>