2014-02-06 4 views
0

프로필 이미지를 업로드 할 수있는 양식이 있지만 프로필 이미지를 제거하려면 옵션을 추가해야합니다. 나는 총 초보자 그렇게 도와주세요 해요, 여기에 코드입니다 : 데이터베이스 연결이 양식이 작동 업로드하고, 상단에 추가 된 데이터베이스의 아바타 이미지 경로입니다 (제안/아바타)이미지를 SQL 데이터베이스에서 삭제

나는 allready 삭제 버튼을 추가
<input name=newimage type=submit value='Change avatar' class=textbox> 
<input name=delimage type=submit value='Remove Avatar' class=textbox> 

$getav = mysql_query("SELECT * FROM suggestions WHERE id = '$ida'"); 
$getavarray = mysql_fetch_array($getav); 
$getav = $getavarray['avatar']; 


if($getav){$avatar = "avatars/$getav";}else{$avatar = "Untitled-1.png";} 

if(isset($_POST['newimage'])){ 
//die(' '); 
//define a maxim size for the uploaded images in Kb 
define ("MAX_SIZE","1000000"); 

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. 
function getExtension($str) { 
     $i = strrpos($str,"."); 
     if (!$i) { return ""; } 
     $l = strlen($str) - $i; 
     $ext = substr($str,$i+1,$l); 
     return $ext; 
} 

//This variable is used as a flag. The value is initialized with 0 (meaning no error found) 
//and it will be changed to 1 if an errro occures. 
//If the error occures the file will not be uploaded. 
$errors=0; 
//checks if the form has been submitted 



    //reads the name of the file the user submitted for uploading 
    $image=$_FILES['image']['name']; 
    //if it is not empty 
    if ($image) 
    { 
    //get the original name of the file from the clients machine 
     $filename = stripslashes($_FILES['image']['name']); 
    //get the extension of the file in a lower case format 
     $extension = getExtension($filename); 
     $extension = strtolower($extension); 
    //if it is not a known extension, we will suppose it is an error and will not upload the file, 
    //otherwise we will do more tests 
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
     { 
     //print error message 
      die('<font color=white face=verdana size=1>Error with the file type. Only JPEG, PNG, GIF and JPG formats allowed.</font>'); 
      $errors=1; 
     } 
     else 
     { 
//get the size of the image in bytes 
//$_FILES['image']['tmp_name'] is the temporary filename of the file 
//in which the uploaded file was stored on the server 
$size=filesize($_FILES['image']['tmp_name']); 

//compare the size with the maxim size we defined and print error if bigger 
if ($size > MAX_SIZE*10024) 
{ 
    die('<font color=white face=verdana size=1>Image size must be 10000kb or less.</font>'); 
    $errors=1; 
} 

//we will give an unique name, for example the time in unix time format 
$image_name=$ida.'.'.$extension; 
//the new name will be containing the full path where will be stored (images folder) 
$newname="avatars/".$image_name; 
//we verify if the image has been uploaded, and print error instead 



$ra = mysql_query("SELECT * FROM suggestions WHERE id= '$ida'"); 
$ras = mysql_fetch_array($ra); 
$rasface = $ras['avatar']; 

$fileaa = "avatars/".$rasface; 

if($rasface){ 
unlink($fileaa); } 


$copied = move_uploaded_file($_FILES['image']['tmp_name'], $newname); 
mysql_query("UPDATE suggestions SET avatar = '$image_name' WHERE id = '$ida'"); 
if (!$copied) 
{ 
    die('<font color=white face=verdana size=1>Error please try again.</font>'); 
    $errors=1; 
} 
else {echo "<font color=white face=verdana size=1>Avatar image changed successfully!</font>";} 


}}} 

: 하지만 나는 아바타를 삭제하기 위해 그것을 어떻게 링크시킬 지 모릅니다.

+2

당신이'select'와'update'은 확실히 당신은 또한 delete' 쿼리 '에 대해 알아야 할 수 있다면 ... –

+0

알아,하지만 무엇이든 나는 그것이 작동하지 않습니다하려고합니다. –

+0

"작동하지 않는 것"을 설명하십시오. 또한 글꼴 태그를 사용하지 마십시오. 네가 할 때마다 타임 버너스 리는 새끼 고양이를 죽인다. – Zarathuztra

답변

0

사이트에 체크 박스를 넣으십시오.

<input name="remove" type="checkbox" value="1" /> 

그런 다음 양식이 제출되면 확인란이 선택되어 있는지 확인하십시오.

// Was the checkbox ticked to mark the deleting of the image? 
if(isset($_POST['remove']) && $_POST['remove'] == 1) 
{ 
    // Do a query to get the image's full path - this is unknown to me, 
    // but you should be able to code this using your database's fields 
    unlink($path); // This will delete the file from the server 

    // Then delete the corresponding row in the database 
    mysql_query("DELETE FROM suggestions WHERE id = 'YOUR_ID_HERE'"); 

} 
관련 문제