2015-12-23 38 views
0

將圖像上傳到sql數據庫我試圖創建一個AdLister網站 - 到目前爲止用戶發佈到數據庫中的所有內容都會在網站上正確顯示,除了圖像。下面是我的代碼 - 請讓我知道如果我留下什麼東西了:如何通過php

$insert_table = "INSERT INTO posts (userid, post_date, title, price, description, email, location, image) VALUES (:userid, :post_date, :title, :price, :description, :email, :location, :image)"; 
    $stmt = $dbc->prepare($insert_table); 
    $stmt->bindValue(':userid', 1, PDO::PARAM_STR); 
    $stmt->bindValue(':post_date', $date, PDO::PARAM_STR); 
    $stmt->bindValue(':title', $title, PDO::PARAM_STR); 
    $stmt->bindValue(':price', $price, PDO::PARAM_STR); 
    $stmt->bindValue(':description', $description, PDO::PARAM_STR); 
    $stmt->bindValue(':email', $email, PDO::PARAM_STR); 
    $stmt->bindValue(':location', $location, PDO::PARAM_STR); 
    $stmt->bindValue(':image', $image, PDO::PARAM_STR); 

    $stmt->execute(); 

    return $errors; 
    } 

    if (!empty($_POST)) { 
     if (checkValues()) { 

     $errors = insertPost($dbc);   
    } else { 
     $message = "Invalid format. Please try again."; 
     $javascript = "<script type='text/javascript'>alert('$message');</script>"; 
     echo $javascript; 
    } 
} 

    if(Input::has('title')){ 
     if($_FILES) { 
      $uploads_directory = '/img'; 
      $filename = $uploads_directory . basename($_FILES['image']['name']); 
      if (move_uploaded_file($_FILES['image']['tmp_name'], $filename)) { 
       // echo '<p>The file '. basename($_FILES['image']['name']). ' has been uploaded.</p>'; 
      } else { 
       //alert("Sorry, there was an error uploading your file."); 
      } 
     } 
     } 
     <table class="table table-hover table-bordered table-striped"> 
     <tr class='table-hover'> 
      <th class="header">Photo</th> 
      <th class="header col-md-1">Date Posted</th> 
      <th class="header">Title</th> 
      <th class="header col-md-1">Price</th> 
      <th class="header col-md-6">Description</th> 
      <th class="header col-md-6">Image</th> 
     </tr> 

      <?php 
      foreach ($posts as $post):?> 
       <tr class='table table-hover table-bordered body'> 
        <td>Photo</td> 
        <td><?= $post['post_date'] ?></td> 
        <td><?= $post['title']?></td> 
        <td><?= $post['price']?></td> 
        <td><?= $post['description']?></td> 
        <td><?= $post['image']?></td> 
      <?php endforeach ?> 
      </tr> 
</table> 
+0

http://php.net/manual/en/function.error-reporting.php –

回答

1

我猜你想插入圖片作爲發佈字符串值(如其他輸入),除了從文件輸入接收數據作爲數組保存名爲$ _FILES

所以......

$_POST['image'] 

將是空的,但

$_FILES['image']['name'] 

將包含實際的文件名。

重新排列代碼以上傳文件並獲取名稱,然後使用該值插入。

哦,不要忘記包括:

enctype="multipart/form-data" 
在HTML表單標籤