2016-10-09 10 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のどちらかです。私のコードで。 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クエリのために必要な変数がsettedされています。
エラーがないはずです。

テキストがなく画像がない場合、スクリプトは何もしません。

+0

ありがとうございました:) – LecheDeCrema

+0

サーbtw、あなたは ' 'のボーダーを隠すことができますか?なぜなら、私は画像をアップロードしていないにもかかわらず、ボーダーがあるからです。- – LecheDeCrema

+0

つまり、データベースに何が表示されているのかは分かりませんが、 ''には境界があります。いくつかの可能なアッ... [こちらをチェック](http://pastebin.com/vugzv0B1)。 –

関連する問題