2016-10-29 3 views
0

PHPデータベースを使用して説明、番号、image pathをアップロードしています。未定義のプロパティ:PHPを使用して画像のパスをアップロードするときのstdClass

controllerからimageを呼び出そうとすると、Undefined property: stdClass::$imgPath .... on line 9というエラーが表示されます。一方、textの要素は正常にアップロードされます。

は以下 PHPのコードを見つけてください:

<?php 
    $prov = json_decode(file_get_contents("php://input")); 
    require_once("connection.php"); 
    $connection = connectToMySQL(); 

    // Check if image file is a actual image or fake image 
     $proverbDescription = $prov->proverbDescription; 
     $proverbNumber = $prov->proverbNumber; 
     $imgPath = $prov->imgPath; 


     $query = "INSERT INTO tbl_proverb (proverbDescription, proverbNumber, imgPath) VALUES ('$proverbDescription', '$proverbNumber', '$imgPath')"; 
     echo($proverbDescription); 
     echo($proverbNumber); 
     echo($imgPath); 
     if (move_uploaded_file($_FILES['imgPath']['tmp_name'], $target_file)) //for uploading file 
     { 
      echo("File uploaded to: " . $target_dir); 
     } 

     $result = mysqli_query($connection, $query) 
      or die("Error in query: ". mysqli_error($connection)); 
     if(mysqli_affected_rows($connection) > 0){ 
       $success = true; 
     }else{ 
       $success = false; 
     } 
?> 

AngularJSコントローラー:

app.controller('proverbCtrl', function($scope, $http, $location) { 

$scope.addProverb = function(prov) { 
    $http.post('php/addProverbs.php', prov).success(function(prov) { 
     console.log(prov); 
    // $location.path("/home"); 
    }); 
}; 
}); 

そして、私のHTMLフォーム:あなたが必要とするすべてのファイルを取得する場合

<form id="demo" class="collapse" ng-submit="addProverb(prov)" enctype="multipart/form-data" method="POST"> 
    <label>Image:</label> 
     <input type="file" ng-model="prov.imgPath" name="imgPath" id="imgPath" accept="image/*"> 
    <label>Proverb Description:</label> 
     <input type="text" ng-model="prov.proverbDescription" ><br><br> 
    <label>Proverb Number:</label>       
     <input type="text" ng-model="prov.proverbNumber"><br><br> 
    <input type="submit" name="submit"><br> 
    <a href="#/proverbs">See your post</a> 
</form> 
+0

あなたがしますprint_r($のPROV)を行う場合は、何を得るのです。 ?? – FRECIA

+0

@LFMarabunta、こんにちは、ありがとうございます。 'stdClassオブジェクト ( [proverbDescription] => testone3 [proverbNumber] => 33 )' – hurkaperpa

答えて

0

PHPの名前を次のように試してください:

HTML:

<input type="file" onchange="angular.element(this).scope().fileName(this)" 
ng-model="prov.imgPath" name="imgPath" id="imgPath" accept="image/*"> 

コントローラー:

$scope.fileName= function(element) { 
    $scope.$apply(function(scope) { 
     scope.prov.imgPath = element.name; 
    }); 
}; 
関連する問題