2016-10-09 54 views
1

如何分開發布或插入照片或文本不在一起到我的數據庫這是MySQL數據庫。如何分別使用php插入照片和文字?

在這行代碼中,我可以同時發佈圖片和文本,但是如果我只想要一張圖片,它會在代碼中顯示錯誤。

<?php 
include('includes/database.php'); 
include('session.php'); 

if (!isset($_FILES['image']['tmp_name'])) { 
    echo ""; 
}else{ 
    $file=$_FILES['image']['tmp_name']; 
    $image = $_FILES["image"] ["name"]; 
    $image_name= addslashes($_FILES['image']['name']); 
    $size = $_FILES["image"] ["size"]; 
    $error = $_FILES["image"] ["error"]; 

    if ($error > 0){ 
     die("Error uploading file! Code $error."); 
    }else{ 
     if($size > 10000000){ //conditions for the file 
      die("Format is not allowed or file size is too big!"); 
     }else{ 

      move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);   
      $location="upload/" . $_FILES["image"]["name"]; 
      $user=$_SESSION['id']; 
      $content=$_POST['content']; 
      $time=time(); 

      $update=mysql_query(" INSERT INTO post (user_id,post_image,content,created) 
      VALUES ('$id','$location','$content','$time') ") or die (mySQL_error()); 

     } 
    header('location:home.php'); 
    } 
} 
?> 

這是對應上述代碼的表單。

<div id="right-nav"> 
    <h1>Update Status</h1> 
    <form method="post" action="post.php" enctype="multipart/form-data"> 
     <textarea placeholder="Whats on your mind?" name="content" class="post-text" required></textarea> 
     <input type="file" name="image"> 
     <button class="btn-share" name="Submit" value="Log out">Share</button> 
    </form> 
</div> 

謝謝你將來的答案。

+0

所以上面的代碼運行良好,但你不想保存'$ content'到數據庫中? –

+0

我想保存它,但我想保存或發佈像Facebook一樣。無論是TEXT還是PHOTO。在我的代碼中。它只會發佈,如果有一個文本和一個照片。 – LecheDeCrema

回答

0

要保存圖像或文本或兩者:

<?php 
include('includes/database.php'); 
include('session.php'); 

$user=$_SESSION['id']; 
$time=time(); 

// Content 
if(isset($_POST['content']) && !empty($_POST['content'])){ 
    $content=$_POST['content']; 
}else{ 
    $content=""; 
} 

// Image 
if (isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name'])) { 
    $file=$_FILES['image']['tmp_name']; 
    $image = $_FILES["image"] ["name"]; 
    $image_name= addslashes($_FILES['image']['name']); 
    $size = $_FILES["image"] ["size"]; 
    $error = $_FILES["image"] ["error"]; 

    if ($error > 0){ 
     die("Error uploading file! Code $error."); 
    } 
    if($size > 10000000){ //conditions for the file 
     die("Format is not allowed or file size is too big!"); 
    } 

    move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);   
    $location="upload/" . $_FILES["image"]["name"]; 
}else{ 
    $location=""; 
} 

// Save to database if there is content OR an image 
// Will save both if both are present 

if(!empty($content) || !empty($location)){ 
    $update=mysql_query(" INSERT INTO post (user_id,post_image,content,created) 
    VALUES ('$id','$location','$content','$time') ") or die (mySQL_error()); 

    // Redirect to home 
    header('location:home.php'); 
} 
?> 

在所有情況下,所需的SQL查詢中的變量設置好的。
所以應該沒有錯誤。

如果沒有文字和圖像,腳本根本不做任何事情。

+0

謝謝先生:) – LecheDeCrema

+0

先生btw,你可以隱藏''的邊界嗎?因爲即使我沒有上傳圖片也沒有邊框-l- – LecheDeCrema

+0

也就是說,我想,當你顯示數據庫中的內容時......「」仍然有一個邊框,因爲它是在CSS中定義的。有幾種可能的答案...... [請點擊此處](http://pastebin.com/vugzv0B1)。 –