2016-02-08 32 views
1

嗨我想要的數據,如標題,說明和image.If只給標題和描述,而不添加圖像的數據應插入到數據庫。但如果我我想這讓error.Here是我的錯誤,代碼:如何插入數據,如果圖像字段爲空使用php

error: error while uploading

我的代碼

$title=$_POST['blog_title']; 
$result = str_replace(" ", "-", $title); 
$description=$_POST['blog_description']; 
$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 
{ 
if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe") 
{ 
move_uploaded_file($temp,"upload/".$name); 
$sql=mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')"); 
echo "upload complete"; 
session_start(); 
header("Location:blogimage.php"); 
} 
else 
{ 
echo "failure"; 
} 

HTML代碼

<form method="POST" action="blogs.php" enctype="multipart/form-data"> 
<div> 
<label for="title">Title</label> 
<input type="text" name="blog_title" value=""> 
</div> 
<div> 
<label for="image">IMAGE</label> 
<input type="file" name="image"> 
</div> 
<div> 
<label for="blog_description">Description</label> 
<textarea name="blog_description" class="text" style="width:50%;"> </textarea> 
</div> 
<input type="submit" value="Submit"/> 
</form> 
+0

發佈您的HTML代碼。 –

+0

@ Mr.Engineer我已經發布了我的html代碼,你可以請檢查它 – user5836176

+0

還在使用'mysql_query'嗎? –

回答

0

首先,在你的PHP腳本的最頂部開始會話,這樣:

<?php 
    session_start(); 
?> 

現在出現了您的問題。首先使用is_uploaded_file()函數檢查文件是否已上傳,然後相應地處理您的表單。

所以,你的代碼應該是這樣的:

$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", "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"; 
    } 
} 

旁註:不要使用mysql_*功能,它們被棄用的PHP 5.5,並且在PHP 7.0完全刪除。改爲使用mysqlipdoAnd this is why you shouldn't use mysql_* functions

+0

非常感謝你這是工作 – user5836176

+0

很高興我能幫上忙。乾杯! :-) –

0

你必須使用如下:

... 
if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe") 
{ 
    move_uploaded_file($temp,"upload/".$name); 
    $sql=mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')"); 
} else { 
    $sql=mysql_query("INSERT INTO blogs(blog_title,blog_description)values('$result','$description')"); 
} 
session_start(); 
header("Location:blogimage.php"); 
... 
+0

仍然使用'mysql_query'? –

+0

@Kausha Mehta它應該插入,如果圖像字段爲空然後那對我沒有用 – user5836176

+0

我將使用mysqli,但現在我需要該圖像的解決方案之一 – user5836176

0

我使用你的代碼mysqli_query,因爲mysql_ *已被棄用:

修改代碼:

<?php 

$link = mysqli_connect("localhost", "root", "", "yourDb"); 
if (!$link) { 
    echo "Error: Unable to connect to MySQL." . PHP_EOL; 
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; 
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; 
    exit; 
} 

$title=$_POST['blog_title']; 
$result = str_replace(" ", "-", $title); 
$description=$_POST['blog_description']; 

$name = ""; 
$failure = ""; 

if(isset($_FILES["image"]["name"])){ 
    $name=$_FILES["image"]["name"]; 
    $type=$_FILES["image"]["type"]; 
    $size=$_FILES["image"]["size"]; 
    $temp=$_FILES["image"]["tmp_name"]; 
    $error=$_FILES["image"]["error"]; 
    if($error>0){ 
     $name = ""; 
    } 
    else{ 
     if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe") 
     { 
      move_uploaded_file($temp,"upload/".$name);   
     } 
    } 
} 

$sql = mysqli_query($link,"INSERT INTO blogs (image,blog_title,blog_description) 
       values('$name','$result','$description')"); 

if($sql){ 
    //echo "upload complete"; 
    session_start(); 
    header("Location:blogimage.php");  
    die(); 
} 
else{ 
    echo 'failure'; 
} 

?> 

說明:

  • 我正在檢查是否設置了$_FILES["image"]["name"]而不是執行文件上傳代碼。
  • 進一步,如果$error不等於0使用move_uploaded_file()
  • 查詢將在默認的兩個文件爲空或不運行,如果空比使用$name爲空否則使用文件名。

從PHP手冊:

mysqli::query - mysqli_query - 執行數據庫

註上查詢,其mysqli_*擴展的程序結構,IST PARAM mysqli_query應該是您的連接標識符,第二參數應該是您的MYSQL語句

+0

它是爲圖像工作,但如果我給任何pdf文件或zip文件它接受它不應該接受任何pdf或zip文件erc ...它應該只接受圖像 – user5836176

+0

@ user5836176:接受其他文件類型與$ type:相關pdf調試你獲得的值是什麼 – devpro

2

根據你的代碼,如果你沒有上傳圖像,$ error的值變爲4.所以你的if()條件得到執行。所以刪除你的條件。

if ($name = $_FILES["image"]["name"] != '') { 
    if ($type == "image/png" || $type == "image/jpg" || $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe") { 
     move_uploaded_file($temp, "upload/" . $name); 
     $sql = mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')"); 
     echo "upload complete";    
    }else{ 
     echo "File type not supported."; 
    } 
session_start(); 
header("Location:blogimage.php"); 
} else { 
    $sql = mysql_query("INSERT INTO blogs(blog_title,blog_description)values('$result','$description')"); 
    echo "upload complete"; 
    session_start(); 
    header("Location:blogimage.php"); 
} 
+0

如果我上傳任何zip文件或pdf文件,它不顯示任何消息 – user5836176

+0

它是不顯示任何錯誤消息 – user5836176

0

你必須讓你的字段和值動態:

試試這個:

$_POST = array('image'=>'','blog_title'=>'yes','blog_description'=>'nothing'); 
foreach ($_POST as $key => $value) { 
    if(!empty($value)){ 
     $fields .= $key.','; 
     $values .= "'".$value."'".','; 
    } 
} 
$fields = substr($fields, 0, -1); 
$values = substr($values, 0, -1); 
echo "INSERT INTO blogs($fields)values($values)"; 
相關問題