2017-08-17 7 views
-1

誰か助けてください。私はdays.Iのためにデバッグされている私はあなたが私を助ける場合私はうれしいだろう問題を抱えています。私の画像はデータベースにアップロードされません

私の画像は自分のデータベースにアップロードできません。それは私にエラーを与えません。それは私のクエリの他のコンポーネントをアップロードして、イメージを残しておきます。私のコードはここにあります

<?php 
global $connection; 
if(isset($_POST['submit'])){ 

    $post_category_id = $_POST['post_category_id']; 
    $post_title = $_POST['post_title']; 
    $post_author = $_POST ['post_author']; 
    $post_status = $_POST['post_status']; 

    $post_image = isset($_FILES['post_image']['image_name']); 
    $post_image_temp = isset($_FILES['post_image']['temp_name']); 

    $post_tags = $_POST['post_tags']; 
    $post_comment_count = 4; 
    $post_date = date('d-m-y'); 
    $post_content = $_POST['post_content']; 
    $post_status = $_POST['post_status']; 
    move_uploaded_file($post_image_temp, '../images/$post_image'); 

    $query = "INSERT INTO posts (post_category_id, post_title, post_author, post_status, post_image, post_tags, post_comment_count, post_date, post_content) VALUES ('{$post_category_id}', '{$post_title}', '{$post_author}', '{$post_status}', '{$post_image}', '{$post_tags}', {$post_comment_count}, now(), '{$post_content}') "; 

    $result = mysqli_query($connection, $query); 
    if ($result){ 
     echo "Post Published"; 
    } else { 
     echo "(Error Code:" . $_FILES['post_image']['error'] . ")"; 
    } 
} 
?> 


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

    <div class="form-group"> 
     <label for="post_title">Title</label> 
     <input type="text" name ='post_title' class="form-control" > 
    </div> 
    <div class="form-group"> 
     <label for="Post_author">Post Author</label> 
     <input type="text" name ='post_author' class="form-control"> 
    </div> 

    <div class="form-group"> 
     <label for="post_category_id">Post Category</label> 
     <input type="text" name ='post_category_id'class="form-control" > 
    </div> 
    <div class="form-group"> 
     <label for="post_status">Post Status</label> 
     <input type="text" name ='post_status' class="form-control"> 
    </div> 
    <div class="form-group"> 
     <label for="post_image">Upload Image</label> 
     <input type="file" name ="post_image" id="post-image" class="form-control" > 
    </div> 
     <div class="post tags"> 
     <label for="post_tags">Post Tags</label> 
     <input type="text" name ='post_tags'class="form-control" > 
    </div> 
    <div class="post comment count"> 
     <label for="post_comment_count">Post Comment Count</label> 
     <input type="text" name ='post_comment_count'class="form-control" > 
    </div> 

    <div class="form-group"> 
     <label for="post_date">post date</label> 
     <input type="date" name ='post_date'class="form-control" > 
    </div> 
    <div class="form-group"> 
     <label for="post_content">Post content</label> 
     <textarea name="post_content" id="" cols="30" rows="10"></textarea> 
    </div> 
    <input class= "btn btn-primary"type="submit" name = "submit" value="Publish Post"> 

</form> 

私はアップロード元の場所にフルコントロールの許可を与えています。あなたはそれなしで私はちょうどあなたがする必要がある私のボタン

+2

SQLインジェクションを防ぐ準備文について学ぶ – Jens

+2

スクリプトが危険にさらされています[SQLインジェクション攻撃](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) [あなたが入力をエスケープしている場合、その安全ではありません!] ://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) [準備されたパラメータ化されたステートメント](http://php.net/manual/en/mysqli .quickstart.prepared-statements.php) – RiggsFolly

+2

issetは変数に値を割り当てません。 –

答えて

0

を提出する上でクリックした後

未定義の変数のエラーを取得するためである私のイメージ名とimage_temp_name上ISSET機能を気付くだろうと確信していますファイルアップロードのコードを変更してください

//check if there is file uploaded 
$post_image = ''; 
if(!empty($_FILES['post_image']) && ($_FILES['post_image']['error']=='0' || $_FILES["pictures"]["error"] == UPLOAD_ERR_OK)){ 
    // get the file name 
    $post_image = $_FILES['post_image']['name']; 
    // get temp name 
    $post_image_temp = $_FILES['post_image']['temp_name']; 
    // code to move uploaded file to desired location on server 
    if (move_uploaded_file($post_image_temp, "../images/$post_image")) { 
     echo "File successfully uploaded.\n"; 
    } else { 
     echo "File not uploaded\n"; 
    } 
} 
+0

ユーザーがファイルを移動すると言った場所にファイルを移動することは非常に悪い考えです。これは、アプリケーションの一部を上書きするために使用できます。代わりに、UUIDのように完全に制御する名前を付けてください。 – tadman

関連する問題