Ionic ngCordova camera upload image mysql+php(having problem to pass object to php:undefine index in php lin 13 title undefined)

here is my controller js code
.controller(‘NewtopicCtrl’, function($scope, $state, $http, $cordovaCamera,$cordovaFileTransfer){

$scope.post = function(topic)
{

// var config = {parms:{title: $topic.title,name: $topic.name, message: $topic.message,file:$scope.imgURI} };
//topic[‘picture’] = $scope.imgURI;
$http.post(“http://localhost:8000/retrive.php?title="+topic.title+" & name=”+topic.name+" & message="+topic.message+"& file="+$scope.imgURI+"")
.success(function(response){
if(response==‘success’){
$state.go(‘tab.listview’);
}else{
alert(response);
}
});
}

$scope.takePicture = function(source)
{
var options = {
quality : 85,
destinationType : Camera.DestinationType.FILE_URL,
sourceType : source,
allowEdit : true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 180,
targetHeight: 180,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false

    };

    $cordovaCamera.getPicture(options).then(function(imageData) {
        $scope.imgURI = "data:image/jpeg;base64," + imageData;
    }, function(err) {
        // An error occured. Show a message to the user
    });
}

});

and the php:

<?php header('Access-Control-Allow-Origin: *'); $server = "localhost"; $username = "root"; $password = ""; $database = "complain_system"; $con = mysqli_connect($server, $username, $password, $database) or die ("Could not connect: " . mysqli_connect_error()); $Title=$_POST["title"]; $Name=$_POST["name"]; $Message=$_POST["message"]; //storing file in filename variable $fileName = $_FILES['file']['name']; //This is the directory where images will be saved $target = "http://localhost:8000/ionic/"; $target = $target . strtolower( $_FILES['file']['name']); mkdir ($target, 0777, true); move_uploaded_file($_FILES['file']['tmp_name'],$target); $sql = "INSERT INTO photo (title,name,message,imgfile) "; $sql .= "VALUES ('$Title','$Name', '$Message', '".strtolower($_FILES['file']['name'])."')";//strlower new base file nam basename if($con->query($sql)){ echo "Your comment has been sent"; } else{ echo "Error in sending your comment"; } ?>

Hello,
the “undefined” is just a notice, not an error. But for your information it says that the variable “title” doesn’t exists in your $_POST. WIth a console.log of your “topic” object in the $scope.post method you can see if topic.title is empty or not.

Btw you are passing the image as text, not an array with title, mimetype etc. (like $_FILES) so your code is wrong. You just have to put the content of $_POST['file'] in a file like this: file_put_contents( 'image.jpg', $_POST['file'] ); and then you can store your filename in the database.

IMPORTANT 1 You are getting values of $_POST without any check and filters. Add addslashes or you will have SQL Injection exploit.

IMPORTANT 2 Before you put your image in a file, you have to check its mimetype. Here is a link about how to do this with a base64 encoded image.

so my php code is wrong or controller?

i change the code…i can save all the form data except the base64image…i think the image url is not sendin to php correctly…so please me to solve this issue… here is the updated code–
$scope.upload = function(input) {

$http.post(“http://192.168.0.3:8000//retrive.php?titles="+input.titles+" & name=”+input.name+"& message="+input.message+" & file="+$scope.imgURI+""). then(function (data) {
$scope.tasks = data;
$state.go(‘tab.listview’);
});

}

$scope.takePicture = function(source)
{
var options = {
quality : 85,
destinationType : Camera.DestinationType.FILE_URL,
sourceType : source,
allowEdit : true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 180,
targetHeight: 180,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false

    };

    $cordovaCamera.getPicture(options).then(function(imageData) {
        $scope.imgURI = "data:image/jpeg;base64," + imageData;
    }, function(err) {
        // An error occured. Show a message to the user
    });
}

});

php:

<?php header('Access-Control-Allow-Origin: *'); $server = "localhost"; $username = "root"; $password = ""; $database = "complain_system"; $con = mysqli_connect($server, $username, $password, $database) or die ("Could not connect: " . mysqli_connect_error()); //if(isset($_POST[submit])){ $Title=$_GET["titles"]; $Name=$_GET["name"]; $Message=$_GET["message"]; //storing file in filename variable $fileName = $_FILES['file']['name']; //This is the directory where images will be saved $target = "http://localhost:8000/ionic/"; $target = $target . strtolower( $_FILES['file']['name']); mkdir ($target, 0777, true); move_uploaded_file($_FILES['file']['tmp_name'],$target); $sql = "INSERT INTO photo (title,name,message,imgfile) "; $sql .= "VALUES ('$Title','$Name', '$Message', '".strtolower($_FILES['file']['name'])."')";//strlower new base file nam basename if($con->query($sql)){ echo "Your comment has been sent"; } else{ echo "Error in sending your comment"; } //} ?>
//storing file in filename variable
 $fileName = $_FILES['file']['name'];

//This is the directory where images will be saved
$target = "http://localhost:8000/ionic/";

$target = $target . strtolower( $_FILES['file']['name']);
mkdir ($target, 0777, true); 
move_uploaded_file($FILES['file']['tmpname'],$target);

$sql = "INSERT INTO photo (title,name,message,imgfile) ";
$sql .= "VALUES ('$Title','$Name', '$Message', '".strtolower($_FILES['file']['name'])."')";//strlower new base file nam basename

Instead of this, try this:

file_put_contents( 'image.jpg', base64_decode( $_POST['file'] ) );

$sql = "INSERT INTO photo (title,name,message,imgfile) ";
$sql .= "VALUES ('$Title','$Name', '$Message', 'image.jpg')";//strlower new base file nam basename

Anyway I think you cannot do $con->query 'cause you are using mysqli_connect as a function, not a class. ( if it works anyway ignore this phrase )

its not working…what do u meant by ‘image.jpg’?
can u help me to solve the issue with controller js and the php code…pls

What is not working? image.jpg is the name ( just for test ) of the image where we put our base64 content from the JS.

yes…“image.jpg” saving database as normal string value

Of course. It’s the same as your strtolower($_FILES['file']['name']). You have to check if the code creates an image.jpg in your dir and the image is ok.