2014-03-06 61 views
1

你好,我有一個形式與許多輸入(名稱,姓氏,圖片,年齡..),我想提交所有這些信息到我的數據庫,我知道如何做一個簡單的輸入,但與圖片,我有一些問題 有人可以幫助我,請我 HTML文件插入圖片到數據庫使用php

<form action="insertion.php" method="post" > 
<table > 
<tr> 
<td><strong>Titre</strong></td> 
<td><input name="titre" type="text" value="" /></td> 
</tr> 
<tr> 
<td><strong>Annee</strong></td> 
<td><input name="annee" type="number" value="" /></td> 
</tr> 
<tr> 
<td><strong>Genre musical</strong></td> 
<td><input name="Gmusical" type="texte" value="" /></td> 
</tr> 
<tr> 
<td> 
<strong>Picture</strong> 
</td> 
<td> 
<input type="file" name="img"/> 
</td> 
</tr> 
</table> 
<input type="submit" value="Submit " /> 
</form> 

我的文件insertion.php到sublmit到數據庫

<?php 
include("connexion.php"); 
$titre=$_POST['titre']; 
$annee=$_POST['annee']; 
$Gmusical=$_POST['Gmusical']; 
$picture=$_POST['img']; 

$req="INSERT INTO `cd` 
(`titre`, `Annee`, `genremusical`, `Image`) 
VALUES 
('$titre','$annee','$Gmusical','$picture');"; 

      if (mysql_query($req)) 
      { 
      echo "ok"; 
      } 
      else 
      echo 'ko'; 
      } 
+1

你可能需要看到這個http://stackoverflow.com/questions/17153624/using-php-to-upload-file-and-add-the-path-to-mysql - 數據庫 –

+3

旁註:爲了處理與圖像相關的表單,你的表單需要包含'enctype ='multipar t/form-data''首先。加上這個'$ picture = $ _ POST ['img'];'應該很可能是'$ picture = $ _ FILES ['img'];' –

+0

您不應該將二進制圖像數據存儲到數據庫。只需將它們保存爲圖像文件並將路徑/文件名保存到數據庫即可。 – djot

回答

3

一般來說,你不希望存儲實際的BLOB(二進制大對象)數據類型s在數據庫中。您將路徑存儲在位於Web服務器上某處的映像中。

因此,在「Image」這一欄中,您將存儲路徑「images/photo1103.jpg」。

要顯示照片: echo "<img src=". $image_query_fetch['Image'] .'" alt=\"\" />";

1

必須設置在塔型在DATABSE到BLOB或LONGBLOB(或others) 「圖像」 列,例如:

CREATE TABLE cd (
... 
Image LONGBLOB, 
); 

然後像你一樣簡單地插入數據。

解決此問題的一種更好的方法是將文件存儲在文件系統中,並僅將文件的路徑保存到數據庫中。如果你想這樣做,你可能想看看this SO question。 (也this one見。)

(如用戶弗雷德-ii-在評論中指出,你也必須設置形式 -Tag爲 「multipart/form-data的」 的ENCTYPE 。)

2

列類型可以是VARCHAR 我也建議使用,而不是MySQL的

的mysqli和下面的代碼應該可以幫助您: 編輯代碼試試這個作爲一個文件,也許嘗試使用echo $picture_name;進行調試,如果你還沒有嘗試過。

可選: 還有一兩件事可以幫助是ob_start();地方這個在文件的最頂端剛好<?php標籤後,比在頁面結束它與ob_flush();前下方?>標籤等你拿是這樣的:enter image description here

<?php 
include('connexion.php'); 

function outout_errors($error) { 
    echo '<ul><li>',$error.'</li></ul>'; 
} 

if($_POST) { 
    $titre = mysql_real_escape_string(strip_tags($_POST['titre'])); 
    $annee = mysql_real_escape_string(strip_tags($_POST['annee'])); 
    $Gmusical = mysql_real_escape_string(strip_tags($_POST['Gmusical'])); 
    $picture_tmp = $_FILES['img']['tmp_name']; 
    $picture_name = $_FILES['img']['name']; 
    $picture_type = $_FILES['img']['type']; 

    $allowed_type = array('image/png', 'image/gif', 'image/jpg', 'image/jpeg'); 

    if(in_array($picture_type, $allowed_type)) { 
     $path = 'images/'.$picture_name; //change this to your liking 
    } else { 
     $error[] = 'File type not allowed'; 
    } 

    if(!is_numeric($annee)) { 
     $error[] = $annee.' is not a number'; 
    } 

    if(!empty($error)) { 
     echo '<font color="red">'.output_errors($error).'</font>'; 

    } else if(empty($error)) { 
     $req="INSERT INTO `cd` (`titre`, `Annee`, `genremusical`, `Image`) VALUES ('$titre', '$annee', '$Gmusical', '$path')"; 
     move_uploaded_file($picture_tmp, $path); 

     if (mysql_query($req)) { 
      echo 'ok'; 
     } else { 
      echo 'ko'; 
     } 
    } 
} 


?> 

<form action="" method="post" enctype="multipart/form-data"> 
    <table> 
    <tr> 
     <td><strong>Titre</strong></td> 
     <td><input name="titre" type="text"></td> 
    </tr> <tr> 
     <td><strong>Annee</strong></td> 
     <td><input name="annee" type="text"></td> 
    </tr><tr> 
     <td><strong>Genre musical</strong></td> 
     <td><input name="Gmusical" type="text"></td> 
    </tr><tr> 
     <td><strong>Picture</strong></td> 
    <td><input type="file" name="img"></td> 
    </tr> 
    <tr> 
     <input type="submit"> 
    </tr> 
    </table> 
</form> 
+0

我試過你的解決方案,但它不工作,我有錯誤:注意:未定義的索引:img在C:\ wamp \ www \ atelier CRUD \ Insertion.php上線16 – Marooweb

+0

我認爲你得到這個通知的原因是因爲變量已經在表單提交之前完成了我已經更改了代碼,我希望現在能夠運行 – SuperDJ

+0

這是否解決了它? – SuperDJ