2014-04-07 46 views
-2

我上傳我的圖片到數據庫,但點擊上傳按鈕後,它沒有上傳到數據庫,它只是刷新,當我手動添加圖片到數據庫中它進入我的數據庫網站,這裏是我的照片點擊上傳按鈕到數據庫後我的圖像沒有上傳

Click here

這裏是源代碼,上傳圖片:

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

mysqli_connect("localhost", "root", "Bhawanku", "members"); 

if(isset($_POST['emp_name'])){ 

    $content=file_get_contents($_FILES['pic']['tmp_name']); 

    @list(, , $imtype,) = getimagesize($_FILES['pic']['tmp_name']); 

    if ($imtype == 3){ 
     $ext="png"; 
    }elseif ($imtype == 2){ 
     $ext="jpeg"; 
    }elseif ($imtype == 1){ 
     $ext="gif"; 
    } 

    $q="insert into employees set empname='".$_POST['emp_name']."',profile_pic='".$content."',ext='".$ext."'"; 
    mysql_query($q); 
    header("location: getting_started.php"); 
} 
?> 

<html> 
<head> 
    <title>Getting started..</title> 
</head> 
<style> 
    body{ 
     background-color: #993333; 
    } 

    #box{ 
     -webkit-box-shadow: 0px 0px 10px 0px rgba(110, 48, 50, 0.75); 
     -moz-box-shadow: 0px 0px 10px 0px rgba(110, 48, 50, 0.75); 
     box-shadow: 0px 0px 10px 0px rgba(110, 48, 50, 0.75); 
     background-color: #F0F0F0; 
     position: fixed; 
     width: 70%; 
     left: 18%; 
     top: 8%; 
     height: 70%; 
    } 

    #user_pic{ 
     position: absolute; 
     top: 18%; 
     left: 12%; 
    } 

    h1{ 
     position: fixed; 
     left: 19%; 
     font-family: Throw My Hands Up in the Air; 
    } 

    #btn_pos{ 
     position: absolute; 
     left: 45%; 
     top: 38%; 
    } 

    #next_btn{ 
     border:1px solid #2a2c2f; -webkit-border-radius: 3px; -moz-border-radius: 3px;border-radius: 3px;font-size:12px;font-family:arial, helvetica, sans-serif; padding: 7px 30px 8px 30px; text-decoration:none; display:inline-block;text-shadow: -1px -1px 0 rgba(0,0,0,0.3);font-weight:bold; color: #FFFFFF; 
     background-color: #45484d; background-image: -webkit-gradient(linear, left top, left bottom, from(#45484d), to(#000000)); 
     background-image: -webkit-linear-gradient(top, #45484d, #000000); 
     background-image: -moz-linear-gradient(top, #45484d, #000000); 
     background-image: -ms-linear-gradient(top, #45484d, #000000); 
     background-image: -o-linear-gradient(top, #45484d, #000000); 
     background-image: linear-gradient(to bottom, #45484d, #000000);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#45484d, endColorstr=#000000); 
     margin: 140px 0px 0px 220px; 
     cursor: pointer; 
    } 

    #btn_upload{ 
     border:1px solid #2a2c2f; -webkit-border-radius: 3px; -moz-border-radius: 3px;border-radius: 3px;font-size:12px;font-family:arial, helvetica, sans-serif; padding: 7px 30px 10px 30px; text-decoration:none; display:inline-block;text-shadow: -1px -1px 0 rgba(0,0,0,0.3);font-weight:bold; color: #FFFFFF; 
     background-color: #45484d; background-image: -webkit-gradient(linear, left top, left bottom, from(#45484d), to(#000000)); 
     background-image: -webkit-linear-gradient(top, #45484d, #000000); 
     background-image: -moz-linear-gradient(top, #45484d, #000000); 
     background-image: -ms-linear-gradient(top, #45484d, #000000); 
     background-image: -o-linear-gradient(top, #45484d, #000000); 
     background-image: linear-gradient(to bottom, #45484d, #000000);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#45484d, endColorstr=#000000); 
     margin: 10px 0px 0px 0px; 
     cursor: pointer; 
    } 

    #img_pos{ 
     position: fixed; 
     top: 20%; 
     left: 22%; 
     width: 25%; 
     height: 30%; 
    } 


</style> 
<body> 
<div id="box"> 
    <h1>Add your profile pic to look better ;)</h1> 
<?php 
$q="select * from employees"; 
$resultset=mysql_query($q); 
if(mysql_num_rows($resultset)==0){ 
    ?> 
    <div id="user_pic"> 
     <img id="img_pos" src="Images/default_user.png" > 
    </div> 
<?php 
} 
while($rec=mysql_fetch_array($resultset)){ 
    ?> 
     <img id="img_pos" src="load_image.php?pic_id=<?php echo $rec['id'];?>" > 
<?php 
} 
?> 

    <div id="btn_pos"> 
    <form method="post" action="" enctype="multipart/form-data"> 
     <input type="file" name="pic"><br> 
     <button id="btn_upload" type="submit">Upload now..</button> 
    </form> 
     <form> 
      <button id="next_btn" type="submit" name="next">Next</button> 
     </form> 
    </div> 
</div> 
</body> 
</html> 

,這裏是數據庫的源代碼:

<?php 
mysql_connect("localhost","root", "Bhawanku"); 
mysql_select_db("members"); 
$q="select * from employees where id=".$_GET['pic_id']; 
$rec=mysql_fetch_array(mysql_query($q)); 
$data=$rec['profile_pic']; 
header('Content-Length: '.strlen($data)); 
header("Content-type: image/".$rec['ext']); 
echo $data; 

編輯: -

您好,我發現我的問題是因爲我沒有使用這個

<input type="text" name="emp_name" /> 

,我刪除了我的代碼,這導致,它根本不會救我的照片進入數據庫,因爲這個代碼,如果我使用它,它會更新我的圖片,所以你可以告訴我沒有被用於我的代碼和圖片將能夠上傳到數據庫請幫助:(因爲所有我的問題是由於這個原因造成的。

+1

* mysqli *和* mysql * extensions的混合使用。選擇一個(提示,這是**不** * * mysql *) – Phil

+0

會告訴我我做錯了什麼? – user3309991

+0

在您的第一個代碼示例中,您使用'mysqli_connect'連接到數據庫(但不要將返回值設置爲任何內容),然後繼續使用'mysql_query'。兩者不兼容。 – Phil

回答

0

您正在上傳二維圖像數據,然後將這些數據合併到要發送到數據庫服務器的文本查詢中。我想象的查詢語法錯誤失敗,但你永遠不知道,因爲你沒有檢查任何錯誤。

作爲第一步查詢行改成這樣:

mysql_query($q) or die(mysql_error()); 

至少會報告任何SQL錯誤。

你不應該真的在數據庫中存儲圖像 - 文件系統更好 - 但如果你必須的話,首先在文件內容上使用base64_encode()

旁註:

  • mysql_*()已被棄用 - 使用mysqli_()PDO代替。
  • 在查詢中使用它們之前,您並未轉義$ _POST變量 - 這是SQL注入的敏感性。作爲最低,請看mysql_real_escape_string()這個,但考慮準備好的語句。

編輯 - 你混合mysql()mysqli()功能。他們是不同的,不能像這樣混合。僅使用'mysqli()'。 (感謝@Phile的那個捕獲)。

+0

)沒有必要使用base64編碼,你可以直接在數據庫中直接存儲二進制數據,參見http:// stackoverflow。com/questions/17/binary-data-in-mysql – Phil

+0

我確實改變了,但仍然無法解決我的問題:( – user3309991

+0

@ user3309991您需要反映您的問題代碼中的任何更改 – Phil