2012-07-13 4 views
0

내가 지금까지 가지고있는 것은 (반복적으로) 반복되는 루프이며, 반복되는 올바른 ID (var_dump에 의해 확인 됨)를 얻지 만 아직 내 sql 쿼리는 ID를 수집하지 않습니다. DELETE 키. 본질적으로 SQL 쿼리는 id = 어레이 반복의 현재 ID 값을 삭제하는 것입니다. 다중 체크 박스를 사용합니다. var 덤프가 ID가 업로드 ID와 일치하는지 확인하지만 삭제를 수행 할 수 없습니다. 여기 코드는 다음과 같습니다mysql 데이터베이스에 대한 foreach PHP 삭제

 function submit_delete() { 
     if(!is_null($_POST['delete']) && !is_null($_POST['checkbox'])) { //Check to see if a  delete command has been submitted. 
    //This will dump your checkbox array to your screen if the submit works. 
    //You can manually check to see if you're getting the correct array with values 
    // var_dump($_POST['checkbox']);//Comment this out once it's working. 
    $id_array = $_POST['checkbox']; 
    //var_dump($id_array); 
    deleteUploads($id_array); 

    } 
    else { 
    echo "Error: submit_delete called without valid checkbox delete.";//var_dump($_POST['checkbox']); 
    } 
} 


function deleteUploads ($id_array) { 
    if (count($id_array) <= 0) { echo "Error: No deletes in id_array!"; echo 'wtf'; } 
    //return; }//bail if its empty 
    require_once ('../mysqli_connect.php'); //Connect to the db 

    $delete_success = false; var_dump($delete_success); 
    foreach ($id_array as $id) { var_dump($id); 
    $remove = "DELETE FROM upload WHERE upload_id= $id";//AND `owner_id`=".$_SESSION['user_id']; 
    $result = mysqli_query ($dbc, $remove); // Run the query 
    // 
    ////$mysqli->query($sql) or die(mysqli_error($mysqli)); 
    if ($result) { $delete_success = true; var_dump($delete_success);} 
    mysqli_close($dbc); 
    } 



    if($delete_success == true) { echo 'done'; 
    header('Location: newwriter_profile.php'); 
    } else { 
    echo "Error: ".mysqli_error(); 
    } 
} 


//Test deleteUploads (remove once you know the function is working) 
//$test_ids = array(); 
//$test_ids[] = 5;//make sure this id is in the db 
//$test_ids[] = 7;//this one too 
submit_delete(); 
//deleteUploads($id_array);//should remove ids 10 and 9// 

mysqli_close($dbc); 
+0

루프가 종료되기 전에 적어도 하나의 삭제를 생성해야합니까? – V1GG3N

답변

1

당신은 당신의 deleteUploads() 기능에서 mysqli_close($dbc); 문을 제거해야합니다. 나는 또한 yhou가 사람들의 대답을 받아들이도록 노력해야한다고 동의한다. 귀하는 9 가지 질문을했으며 13 가지 대답 중 하나를 수락하지 않았습니다. 이것은 공정한 게임이 아닙니다.

편집 저는 신속하게 스크립트를 실행하여 조금 옮겼습니다. 유용한 정보가 있습니까?

<? 
$msgs[] = 'Log: Started'; 
require_once('../mysqli_connect.php'); 

function submit_delete() 
{ 
    global $msgs; 
    if(!is_null($_POST['delete']) && !is_null($_POST['checkbox'])) 
    { 
     $msgs[] = "Log: submit_delete called with valid checkbox delete."; 
     $id_array = $_POST['checkbox']; 
     deleteUploads($id_array); 
    }else{ 
     $msgs[] = "Error: submit_delete called without valid checkbox delete."; 
    } 
} 

function deleteUploads ($id_array) 
{ 
    global $msgs; 
    if (count($id_array) <= 0) 
    { 
     $msgs[] = "Error: No deletes in id_array!"; 
    }else{ 
     $msgs[] = "Log: Deletes in id_array!"; 
    } 

    $delete_success = false; 
    foreach ($id_array as $id) 
    { 
     $msgs[] = "Log: Processing id: ".$id; 
     $remove = "DELETE FROM upload WHERE upload_id = $id"; 
     $result = mysqli_query ($dbc, $remove); 
     if ($result) 
     { 
      $msgs[] = 'Log: delete success = true'; 
      header('Location: newwriter_profile.php'); 
     }else{ 
      $msgs[] = 'Error: '.mysqli_error(); 
     } 
    } 
} 

submit_delete(); 

if([email protected]_close($dbc)) 
{ 
    $msgs[] = 'Error: mysqli_close failed'; 
} 
echo implode('<br>',$msgs); 
?> 
+0

또한 루프가 DELETE를 적어도 한 번 이상 실행해야합니까? 연결을 닫기 전에? – V1GG3N

+0

방금 ​​문제가 해결되지 않았 음을 확인했으며, mysqli_close를 제거한 후에는 DELETE가 발생하지 않습니다. – V1GG3N

관련 문제