2016-02-06 47 views
1

我正在寫查詢接受圖像字段只有svg,png,jpeg format.But如果圖像字段爲空,它顯示爲上傳時的錯誤,實際上它應該插入剩餘的字段,如名稱和所有如果我不添加圖像also.Here是我的查詢。應該接受數據如果圖像字段爲空使用php

使用example.php

$title=$_POST['blog_title']; 
$result = str_replace(" ", "-", $title); 
$description=$_POST['blog_description']; 
if(is_uploaded_file($_FILES['image']['tmp_name'])){ 
$name=$_FILES["image"]["name"]; 
$type=$_FILES["image"]["type"]; 
$size=$_FILES["image"]["size"]; 
$temp=$_FILES["image"]["tmp_name"]; 
$error=$_FILES["image"]["error"]; 
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); 
if($error > 0){ 
    die("error while uploading"); 
}else{ 
    $permissible_extension = array("png", "jpg", "jpeg", "svg", "jpg","jpe"); 
    if(in_array($ext, $permissible_extension)){ 
     if(move_uploaded_file($temp,"upload/".$name)){ 
      $sql = mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')"); 
      if($sql){ 
       header("Location:blogimage.php"); 
       exit(); 
      }else{ 
       echo "Insertion failed"; 
      } 
     }else{ 
      echo "File couldn't be uploaded"; 
     } 
    }else{ 
     echo "Invalid format"; 
    } 
} 
}else{ 
$sql = mysql_query("INSERT INTO blogs(blog_title,blog_description)values('$result','$description')"); 
if($sql){ 
    header("Location:blogimage.php"); 
    exit(); 
}else{ 
    echo "Insertion failed"; 
} 
} 
+0

「**應該上傳的圖片,如果沒有圖像**」它是有意義的嗎? –

+0

如果iam沒有添加圖片,我提交了記錄,那麼它應該插入正確的東西,這對你有意義嗎? – user5891511

+1

你能跟我們分享顯示的錯誤嗎? – drosam

回答

0

首先,反對使用mysql_*功能強制性建議。這已棄用,請改用mysqli_*functionsPDO

現在,你的問題。如果您想要存儲關於文件的其他信息,即使文件類型爲空或錯誤,您也需要在測試之前執行INSERT。

下面是一些代碼來嘗試:

<?php 
//connect to the database using mysqli 
$name=$_FILES["image"]["name"]; 
$type=$_FILES["image"]["type"]; 
$size=$_FILES["image"]["size"]; 
$temp=$_FILES["image"]["tmp_name"]; 
$error=$_FILES["image"]["error"]; 
if($error>0) 
    die("error while uploading"); 
else 
{ 
    //Insert the informations in your database here, so even without the upload, the info about the file will be stored 
    $query = mysqli_query("INSERT INTO blogs(image)values('$name')"); 
    if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe" || $type==" ") 
    { 
     move_uploaded_file($temp,"upload/".$name); 
     echo "upload complete"; 
    } 
    else 
    { 
     die("Format not allowed or file size too big!"); 
    } 
} 
+0

不,它不是在我的db iam工作,有三列名稱,圖像和描述,如果我不會選擇圖像,並只給出名稱和描述,然後在上傳時出現錯誤。 – user5891511

+0

請任何人都可以幫助我,如果圖像字段爲空也應該插入數據到其餘列的數據庫。 – user5891511

相關問題