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

**URL:** <https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417>\
**Category:** cordova\
**Created:** [May 7, 2016, 8:59am UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417 "2016-05-07T08:59:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![khanimrul46](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@khanimrul46](https://forum.ionicframework.com/u/khanimrul46)\
**Post date:** [May 7, 2016, 8:59am UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/1 "2016-05-07T08:59:04Z")

</div>

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](http://topic.name), message: $topic.message,file:$scope.imgURI} };  
//topic[‘picture’] = $scope.imgURI;  
$http.post(“[http://localhost:8000/retrive.php?title="+topic.title+"](http://localhost:8000/retrive.php?title=%22+topic.title+%22) & 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"; } ?\>

---

<div class="post-metadata">

**Author:** ![Grork](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/grork/32/139467_2.png) [@Grork](https://forum.ionicframework.com/u/Grork)\
**Post date:** [May 7, 2016, 11:53am UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/2 "2016-05-07T11:53:19Z")

</div>

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](http://stackoverflow.com/a/6061602/1973205) is a link about how to do this with a base64 encoded image.

---

<div class="post-metadata">

**Author:** ![khanimrul46](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@khanimrul46](https://forum.ionicframework.com/u/khanimrul46)\
**Post date:** [May 7, 2016, 12:13pm UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/3 "2016-05-07T12:13:35Z")

</div>

so my php code is wrong or controller?

---

<div class="post-metadata">

**Author:** ![khanimrul46](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@khanimrul46](https://forum.ionicframework.com/u/khanimrul46)\
**Post date:** [May 7, 2016, 12:58pm UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/4 "2016-05-07T12:58:11Z")

</div>

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+"](http://192.168.0.3:8000//retrive.php?titles=%22+input.titles+%22) & 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"; } //} ?\>

---

<div class="post-metadata">

**Author:** ![Grork](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/grork/32/139467_2.png) [@Grork](https://forum.ionicframework.com/u/Grork)\
**Post date:** [May 7, 2016, 4:23pm UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/5 "2016-05-07T16:23:10Z")

</div>

```
//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 )

---

<div class="post-metadata">

**Author:** ![khanimrul46](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@khanimrul46](https://forum.ionicframework.com/u/khanimrul46)\
**Post date:** [May 7, 2016, 8:36pm UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/6 "2016-05-07T20:36:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Grork](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/grork/32/139467_2.png) [@Grork](https://forum.ionicframework.com/u/Grork)\
**Post date:** [May 7, 2016, 8:38pm UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/7 "2016-05-07T20:38:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![khanimrul46](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@khanimrul46](https://forum.ionicframework.com/u/khanimrul46)\
**Post date:** [May 8, 2016, 5:13am UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/8 "2016-05-08T05:13:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Grork](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/grork/32/139467_2.png) [@Grork](https://forum.ionicframework.com/u/Grork)\
**Post date:** [May 8, 2016, 9:49am UTC](https://forum.ionicframework.com/t/ionic-ngcordova-camera-upload-image-mysql-php-having-problem-to-pass-object-to-php-undefine-index-in-php-lin-13-title-undefined/51417/9 "2016-05-08T09:49:21Z")

</div>

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.
