2016-12-04 14 views
-1

私はクラス内の最終プロジェクトのためにソーシャルメディアのウェブサイトを作っています。このページでは、ユーザーが壁のポストに画像を挿入してイメージできるようにしようとしています。私は、ファイルを取り込むhtml形式のコードを持っています。それは、それが自分自身であるときには完全に動作しますが、私のコードに入れようとすると、PHPの$ _FILES配列は常に空です。私はすでにphp.iniファイルの最大ポストサイズと最大入力サイズが十分に大きいことを確認しましたが、この時点で何をすべきか分からず、stackoverflowの他のポストごとにすべての提案を試みました。このここに入力HTMLファイルの入力が機能していません

<form method="post" enctype="multipart/form-data"> 

    Select image to upload: 
    <input type="file" name="myFile"> 

    <input type="submit" value="Upload!" name="submitForm" /> 

</form> 

のための私のフォームは、私は、フォームとPHPのコードはI場合は正常に動作、それを処理するために言ったように

if (isset($_POST['submitForm'])) { 


print_r($_FILES); 

}

フォームを処理するPHPコードですそれらを自分のファイルに入れてください。ファイルに入れたら、実際には使用したくはありません。ここにファイル全体があります。他のコードが壊れていると思われます。

 <?php 

    if (isset($_POST['submitForm'])) { 


     print_r($_FILES); 


    } 

    session_start(); 
    include "../includes/dbConnection.php"; 
    $dbConn = getDatabaseConnection("book_face"); 


    //if user not signed in go to login 
    if (!isset($_SESSION['username'])){ 
     header('Location: signin.php'); 
    } 
    //print_r($_FILES); 



    function getSignedInUser($username){ 
     global $dbConn; 
     $namedParameters = array(); 
     $namedParameters[':username'] = $username; 

     $sql = "SELECT * FROM user 
       WHERE username = :username"; 
     $statement = $dbConn->prepare($sql); 
     $statement->execute($namedParameters); 
     $record = $statement->fetch(PDO::FETCH_ASSOC); 

     return $record; 
    } 

    //determines if signed in user is friends with this user 
    function isFriends($signedInId, $userId){ 
     global $dbConn; 
     $namedParameters = array(); 
     $namedParameters[':user1'] = $signedInId; 
     $namedParameters['user2'] = $userId; 

     $sql = "SELECT * FROM friends 
       WHERE user1 = :user2 
       AND user2 = :user1"; 
     $statement = $dbConn->prepare($sql); 
     $statement->execute($namedParameters); 
     $record = $statement->fetchAll(PDO::FETCH_ASSOC); 

     if (empty($record)){ 
      return false; 
     }else{ 
      return true; 
     } 
    } 

    //gets records of the user from the given id 
    function getProfileInfo($id){ 
     global $dbConn; 

     $namedParameters = array(); 
     $namedParameters[':id'] = $id; 

     $sql = "SELECT * FROM user 
       WHERE id = :id"; 
     $statement = $dbConn->prepare($sql); 
     $statement->execute($namedParameters); 
     $record = $statement->fetch(PDO::FETCH_ASSOC); 

     return $record; 
    } 



    //gets the user friends ti display in friends div 
    function getUsersFriends($id){ 
     global $dbConn; 
     $namedParameters = array(); 
     $namedParameters[':id'] = $id; 
     /* 
     $sql = "SELECT *, user.id FROM friends 
     JOIN user ON user.id = friends.user2 OR user.id = friends.user1 
     WHERE user1 = :id OR user2 = :id"; 
     */ 
     $sql = "SELECT *, user.id FROM user 
       JOIN friends ON friends.user1 = user.id OR friends.user2 = user.id 
       WHERE friends.user1 = user.id OR friends.user2 = user.id"; 

     $statement = $dbConn->prepare($sql); 
     $statement->execute($namedParameters); 
     $records = $statement->fetchALL(PDO::FETCH_ASSOC); 
     //deleteDuplicates($records); 
     $counter = 0; 
     $unique = array(); 
     foreach($records as $record){ 
      //print_r($record); 
      //echo $record['id']; 
      if (array_search($record['id'],$unique) == false && $record['id'] != $id && ($record['user1'] == $id || $record['user2'] == $id)){ 
       //echo $record['id']; 
       //echo "true"; 

       $unique[] = $record['id']; 
      } 
     } 
     $unique = array_unique($unique); 
     //print_r($unique); 
     echo "<h3 style='text-align:center'>Friends:</h3>"; 
     for ($i = 0; $i < sizeof($unique);$i++){ 

      $record = getProfileInfo($unique[$i]); 
      //print_r($record); 
      echo "<div class='searchResult' id='".$record.['id']."' onclick='getResultId(".$record['id'].")'>"; 
        //echo "<span>" . $record['firstName'] . " " . $record['lastName'] . " " . $record['username'] . "</span>"; 
        echo "<img class='profilePic' src='img/".$record['avatar'] . "'>"; 
        echo "<p>"; 
        echo "Name: " . $record['firstName'] . " " . $record['lastName'] . "</br>"; 
        echo "Username: " . $record['username'] . "</br>"; 
        echo "Lives in: " . $record['state'] . "</br>"; 
        echo "<a href='profile.php?id=".$record['id']."'>View Profile</a>"; 
        echo "</p>"; 
       echo "</div>"; 
     } 
    } 

    $signedInUser = getSignedInUser($_SESSION['username']); 
    $profileId = $_GET['id']; //id of curren users profile 
    $profileRecords = getProfileInfo($profileId); //records of current profile 


    //displas record info of inpued user 
    function displayProfileInfo($record){ 
     global $signedInUser; 
     global $profileRecords; 


     echo "<img style='margin:0 auto' class='profileAvatar' src='img/".$record['avatar'] . "'></br>"; 
     echo "<div style='margin:0 auto;margin:10px;text-align:center;background-color:white;border:2px solid black'>"; 
     echo "<h3 id='profileName' style='text-align:center'>".$record['firstName']. " " . $record['lastName']. "</h3>"; 
     echo "<h3 id='profileName' style='text-align:center'>Username: ".$record['username']. "</h3>"; 
     echo "<h3 id='profileName' style='text-align:center'>Lives in: ".$record['state']. "</h3>"; 
     echo "<h3 id='profileName' style='text-align:center'>".$record['age']. " years old" . "</h3>"; 
     echo "<h3 id='profileName' style='text-align:center'>Gender: ".$record['gender'] . "</h3>"; 
     echo "</div>"; 
     if (!isFriends($signedInUser['id'],$record['id']) && $signedInUser['id'] != $profileRecords['id']){ 
      echo "<button type='button' style='margin:0 auto;text-align:center;margin-left:200px' class='btn btn-default' id='addFriend' onclick='var user1 = ".json_encode($signedInUser['id']).";var user2 = ".json_encode($record['id']).";addFriend(user1,user2);'>Add Friend</button>"; 
     }else{ 
      echo "<button type='button' style='margin:0 auto;text-align:center;margin-left:200px' class='btn btn-default' id='removeFriend' onclick='var user1 = ".json_encode($signedInUser['id']).";var user2 = ".json_encode($record['id']).";removeFriend(user1,user2);'>Remove Friend</button>"; 
     } 

    } 



    function getUserPosts($id){ 



     global $dbConn; 
     global $signedInUser; 
     global $profileRecords; 
     $namedParameters = array(); 
     $namedParameters[':id'] = $id; 

     if ($id == $signedInUser['id']){ 
      /* 
      echo "<div style='width:90%;height:10%'>"; 
       echo "<h3 style='text-align:center'>Create new post</h3>"; 
       //echo "<textarea id='postContent' name='postContent'>What's on your mind?</textarea>"; 
      echo "</div>"; 
      */ 
      echo "<html><html>"; 
      echo "<div id='theNewPost' class='post'>"; 
       echo "<div style='float:right' class='btn-group' role='group'>"; 
        //echo "<button id='textPost' type='button' class='btn btn-default' onclick='changePost(1)'>Text Post</button>"; 
        //echo "<button id='imagePost' type='button' class='btn btn-default' onclick='changePost(0)'>Image Post</button>"; 
      echo "</div>"; 
       echo "<h3 style='text-align:justify;margin-left:50px'>Create new post</h3>"; 

       echo "<textarea id='postContent' name='postContent'>What's on your mind?</textarea>"; 
       echo "<button type='button' style='float:right;margin-right:100px;margin-top:20px' class='btn btn-default' id='newPostBtn' onclick='var id=".json_encode($signedInUser['id']).";addPost(id);'>Post</button>"; 
       echo "<span id='postError' class='error' style='clear:both'></span>"; 
      echo "</div>"; 
     } 

     $sql = "SELECT * FROM post 
     WHERE authorId = :id"; 

     $statement = $dbConn->prepare($sql); 
     $statement->execute($namedParameters); 
     $records = $statement->fetchALL(PDO::FETCH_ASSOC); 

     foreach ($records as $record){ 
      echo "<div class='homePostSearchResult'>"; 
       echo "<img src='img/".$profileRecords['avatar']."' />"; 
       echo "<p style='margin-left:20px;margin-top:20px'>".$record['content']."</p>"; 
       echo "<button type='button' style='float:right;margin-right:50px' class='glyphicon glyphicon-hand-right' id='".$record['id']."' value='".$record['likes']."'onclick='var id=".json_encode($record['id']).";likePost(id)'> Likes: ".$record['likes']."</button>"; 
      echo "</div>"; 
     } 
    } 



    ?> 

    <!DOCTYPE html> 
    <html> 
     <head> 
      <title> </title> 
      <link type="text/css" rel="stylesheet" href="css/Project2.css"> 
      <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> 
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
      <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> 
      <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> 
      <script type="text/javascript" src="data.json"></script> 
      <script> 
       /* 
       function changePost(type){ 
        alert(type); 
        if (type == 0){ 
         $("#postContent").css("display","none"); 
         $("#imgUploadForm").css("display","block"); 
         $("#file").css("display","block"); 
         $("#selectImgSpan").css("display","block"); 
         $("#fileSubmit").css("display","block"); 
        }else{ 
         $("#postContent").css("display","block"); 
         $("#imgUploadForm").css("display","none"); 
         $("#file").css("display","none"); 
         $("#selectImgSpan").css("display","none"); 
         $("#fileSubmit").css("display","none"); 
        } 

       }*/ 
       function signOut(){ 
        window.location="signin.php"; 
       } 

       function removeFriend(user1,user2){ 
        $.ajax({ 
         type: "post", 
         url: "removeFriend.php", 
         dataType: "json", 
         data: { "user1": user1, "user2": user2 }, 
         success: function(data,status) { 
           alert(data); 
           window.location.reload(); 
          }, 
          complete: function(data,status) { //optional, used for debugging purposes 
           //alert(status); 
          } 
        }); 
       } 

       function likePost(id){ 
        $.ajax({ 
         type: "post", 
         url: "likePost.php", 
         dataType: "json", 
         data: { "id":id, "likes":$("#" + id).val() }, 
         success: function(data,status) { 

           $("#" + id).val(parseInt($("#" + id).val()) + 1); 
           $("#" + id).html(" Likes: " + $("#" + id).val()); 
          }, 
          complete: function(data,status) { //optional, used for debugging purposes 
           //alert(status); 
          } 
        }); 
       } 

       function addFriend(user1, user2){ 
        $.ajax({ 
         type: "post", 
         url: "addFriend.php", 
         dataType: "json", 
         data: { "user1": user2, "user2": user1 }, 
         success: function(data,status) { 
           alert(data); 
           window.location.reload(); 
          }, 
          complete: function(data,status) { //optional, used for debugging purposes 
           //alert(status); 
          } 
        }); 
       } 

       function addPost(id){ 
        alert(id); 
        alert($('input[type=file]')[0].files[0].name) 
        if (validatePost()){ 
         $.ajax({ 
          type: "post", 
          url: "addPost.php", 
          dataType: "json", 
          data: { "postContent": $("#postContent").val(), "id": id }, 
          success: function(data,status) { 
            alert("Successfully posted!"); 
            window.location.reload(); 
           }, 
           complete: function(data,status) { //optional, used for debugging purposes 
            //alert(status); 
           } 
         }); 
        }else{ 
         alert("Invalid Post"); 
        } 
       } 

       function validatePost(){ 
        var isValid = true; 
        $("#postError").html(""); 

        if ($("#postContent").val().trim().length == 0){ 
         isValid = false; 
         $("#postError").html("Post cannot be blank"); 
        } 
        if ($("#postContent").val().trim().length > 40){ 
         isValid = false; 
         $("#postError").html("Post cannot exceed 40 characters"); 
        } 

        return isValid; 
       } 



       $(document).ready(function(){ 
       $("#friends").css("display","block"); 
       $("#postContent").change(function(){ 
        validatePost(); 
       });//username changes 
       }); 

      </script> 
     <nav> 
       <form id="homeSearch" method="post"> 
        <?php 
        if (isset($_SESSION['username'])){ 
         echo"<p style=';font-size:18px;position:absolute;top:0;right:0;color:white' class='navbar-text navbar-right'>Signed in as <a style='color:orange' href='profile.php?id=".$signedInUser['id']."'' class='navbar-link'>".$signedInUser['username']."</a></p>"; 
        } 

        ?> 
        <span id="bookFace">BookFace</span> 



        <div style="float:right" class="dropdown"> 

        <button style="margin-top:0;margin-left:0" class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 
        Navigate 
        <span class="caret"></span> 
        </button> 

        <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> 
        <li><a href="home.php">Home</a></li> 
        <?php 
         if (isset($_SESSION['username']) && $_SESSION['username'] == "ZMark"){ 
          echo "<li><a href='admin.php'>Admin Page</a></li>"; 
          echo "<li><a href='statistics.php'>Statistics</a></li>"; 
         } 
        ?> 
        <?php 
         if (isset($_SESSION['username'])){ 
          echo "<li><a href='profile.php?id=".$profileRecords['id']."'>Profile</a></li>"; 
          echo "<li role='separator' class='divider'></li>"; 
          echo "<li><a href='signin.php'>Sign Out</a></li>"; 
         }else{ 
          echo "<li><a href='signin.php'>Sign In</a></li>"; 
          echo "<li><a href='signup.html'>Sign Up</a></li>"; 
         } 
        ?> 
        </ul> 
       </div> 





      </nav> 
     </head> 
     <form method="post" enctype="multipart/form-data"> 

      Select image to upload: 
      <input type="file" name="myFile"> 

      <input type="submit" value="Upload!" name="submitForm" /> 

     </form> 

     <body style="background-color:#d7dde5"> 
      <div id="profileInfo"> 
       <?php 

        displayProfileInfo($profileRecords); 
       ?> 
      </div> 
      <div id="friends"> 

       <?php 
        getUsersFriends($profileRecords['id']); 
       ?> 
      </div> 
      <div id= "friendPosts"> 
       <?php 
        getUserPosts($profileRecords['id']); 

       ?> 
      </div> 
     </body> 
    </html> 

    <?php 

    ?> 
+2

質問には関係のないコードがたくさんあります。この問題を再現する最小限に抑えることができますか? [MCVE](http://stackoverflow.com/help/mcve)を参照してください –

+0

フォームをあなたの 'PHP'ページに送りますか? –

+0

申し訳ありませんが、あなたはそれが何を意味するのか分かりません。あなたはフォームが処理するために別のPHPファイルにデータを送信するかどうか尋ねていますか?それがあなたが何を意味していたのであれば、フォームにはアクションやonsubmit機能はありません。 –

答えて

0

あなたのタグには何もアクションはありませんか? これを試すことができます

<form method="post" enctype="multipart/form-data" action="your_action.php"> 

//your form 

</form> 
+0

'action'属性は厳密に必須ではありません。省略された場合、フォームは別の方法で上書きされない限り、現在のURLにポストされます。 https://www.w3.org/TR/html5/forms.html#attributes-for-form-submissionをご覧ください。 –

関連する問題