2016-11-05 12 views
0

イメージをデータベースに保存しようとしましたが、動作しません。それはテキストでは機能しますが、画像のアップロードでは機能しません。フォームでテキストと画像をアップロードし、パスとデータベースに保存する

投稿をクリックすると、同じページを更新するだけで何もしません。画像コードを使用していない場合は、データベースにテキストを保存することができます。

どうしたのですか?これはPHPこれは私の厄介な英語

<form method="POST"> 
     <input type="hidden" name="verzonden" value="1" /> 
     <input type="text" name="titel" required placeholder="Titel" maxlength="20"> 
     <br> 
     <textarea rows="13" cols="30" required name="content" placeholder="Content" id="tekst" maxlength="400"></textarea> 
     <br> 
     <input type="file" id="afbeelding" accept="image/jpeg, image/x-png, image/gif" required> 
     <br> <br> 
     <input type="submit" name="submit" value="Opslaan"> 
</form> 

申し訳ありませんが、フォームにある

<?php 

include("connect.php"); 


if(isset($_POST['titel']) && isset($_POST['content']) && isset($_POST['afbeelding'])){ 

    $titel = $_POST['titel']; 
    $content = $_POST['content']; 
    $afbeelding = $_POST['afbeelding']; 

    $tmp = $_FILES['afbeelding']['tmp_name']; 
    $file_location = 'gallery/'.date("YmdHis").'_'.$_FILES['afbeelding']['name']; 

    move_uploaded_file($tmp, $file_location); 

    $sql = "INSERT INTO `portfolio_items` (titel, content, date, afbeelding, active) VALUES (:titel, :content, NOW(), :afbeelding, 1)"; 

    $query = $conn->prepare($sql); 
    $query->bindParam(':titel', $titel, PDO::PARAM_STR); 
    $query->bindParam(':content', $content, PDO::PARAM_STR); 
    $query->bindParam(':afbeelding', $afbeelding, PDO::PARAM_STR); 
    $query->execute(); 

    echo "Opgeslagen!"; 

} 

ある

を助けてください、それは私の母国語

あなたのコード内のいくつかの行の変更が見
+0

あなたのformタグ – RST

+0

そして、実際に ''のenctype = "multipart/form-data" を追加 - データベースで保存する画像?なぜファイルシステム上に画像を保存するのではなく、データベースに格納されている場所へのリンク(または実際にはイメージ名だけです)は、ファイルシステムのロジックがWebサーバー上で実行されるだけなので、 – junkfoodjunkie

+0

申し訳ありませんが、データベースへの画像のパスを保存することを意味します。 – Terry

答えて

0

ではありません以下:

<?php 

include("connect.php"); 


if(isset($_POST['titel']) && isset($_POST['content']) && isset($_FILES['afbeelding'])){ 

$titel = $_POST['titel']; 
$content = $_POST['content']; 

$tmp = $_FILES['afbeelding']['tmp_name']; 
$file_location = 'gallery/'.date("YmdHis").'_'.$_FILES['afbeelding']['name']; 

    move_uploaded_file($tmp, $file_location); 

$sql = "INSERT INTO `portfolio_items` (titel, content, date, afbeelding, active) VALUES (:titel, :content, NOW(), :afbeelding, 1)"; 


$query = $conn->prepare($sql); 
$query->bindParam(':titel', $titel, PDO::PARAM_STR); 
$query->bindParam(':content', $content, PDO::PARAM_STR); 
$query->bindParam(':afbeelding', $_FILES['afbeelding']['name'], PDO::PARAM_STR); 
$query->execute(); 

echo "Opgeslagen!"; 

} 

フォームコード

<form method="POST" enctype="multipart/form-data"> 
     <input type="hidden" name="verzonden" value="1" /> 
     <input type="text" name="titel" required placeholder="Titel" maxlength="20"> 
     <br> 
     <textarea rows="13" cols="30" required name="content" placeholder="Content" id="tekst" maxlength="400"></textarea> 
     <br> 
     <input type="file" id="afbeelding" name="afbeelding" accept="image/jpeg, image/x-png, image/gif" required> 
     <br> <br> 
     <input type="submit" name="submit" value="Opslaan"> 
    </form> 
+0

その部分を表示するだけであれば、変更した内容がより明白になります。 – RST

+0

まだ動作しません、ファイルは 'gallery /'にアップロードされず、データベースには何も保存されません – Terry

関連する問題