2017-10-06 47 views
-3

我正在嘗試使用此語句更新數據庫中的頭像..我不知道是什麼錯誤,但沒有任何幫助,我將不勝感激。在php中更新頭像

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

    $adminID = $_SESSION['adminID']; 

    $type = explode('.', $_FILES['avatar']['name']); 
    $type = $type[count($type)-1];  
    $url = 'assests/images/users/'.uniqid(rand()).'.'.$type; 
    if(in_array($type, array('gif', 'jpg', 'jpeg', 'png', 'JPG', 'GIF', 'JPEG', 'PNG'))) { 
     if(is_uploaded_file($_FILES['avatar']['tmp_name'])) {   
      if(move_uploaded_file($_FILES['avatar']['tmp_name'], $url)) { 

       try {           
         //insert into database with a prepared statement 
         $stmt = $db->prepare("UPDATE admin SET avatar = '$url' WHERE adminID = $adminID"); 
         $stmt->execute(array(

         )); 
       //else catch the exception and show the error. 
        } catch(PDOException $e) { 
         $error[] = $e->getMessage(); 
         }      
      }  
     } 
    } 
} 
+0

怎麼了?你期待的結果是什麼,你會得到什麼? –

+0

我期待它將$ url插入數據庫 –

+0

是的,但是會發生什麼呢?任何錯誤? –

回答

0

經過研究和閱讀,我已經能夠得到它的工作感謝所有。

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

    $adminID = $_SESSION['adminID']; 

    $type = explode('.', $_FILES['avatar']['name']); 
    $type = $type[count($type)-1];  
    $url = 'assets/images/users/'.$_SESSION['username'].'.'.$type; 
    if(in_array($type, array('gif', 'jpg', 'jpeg', 'png', 'JPG', 'GIF', 'JPEG', 'PNG'))) { 
     if(is_uploaded_file($_FILES['avatar']['tmp_name'])) {   
      if(move_uploaded_file($_FILES['avatar']['tmp_name'], $url)) { 

       try { //insert into database with a prepared statement 
         $stmt = $db->prepare("UPDATE admin SET avatar = '$url' WHERE adminID = $adminID"); 
         $stmt->execute(array(
          $adminID 
         )); 

         echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$location.'">'; 
         exit; 
        } 
       //else catch the exception and show the error. 
       catch(PDOException $e) { 
         $error[] = $e->getMessage(); 
        }      
      }  
     } 
    } 
}