2012-05-16 334 views
0

我非常接近解決我的問題,這是我在哪裏:庫MySQLi的foreach循環刪除麻煩

  while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)) 
    { 
     echo '<tr><td align="left">' . 
     $row['title'] . '</td><td align="left">' 
     . $row['genre'] . '</td><td align="left">' 
     . $row['length'] . '</td><td align="left">' 
     . $row['created'] . '</td><td align="left">' 
     . $row['views'] . '</td><td align="left">' //. var_dump($row) //dump row value for testing 
     . '<input type="checkbox" name="checkbox[]" value= "'.$row['upload_id'].'"'.' />'.' </td>'. '</tr>'; 



    } 
    echo '</table>'; // Close the table 

和刪除功能:

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. 
    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!"; var_dump($_POST['checkbox']); return; }//bail if its empty 
     $delete_success = false; 
     foreach ($id_array as &$id) { 
     $sql = "DELETE FROM `upload` WHERE `upload_id`=$id AND `owner_id`={$_SESSION['user_id']}"; 
     $result = $mysqli->query($sql) or die(mysqli_error($mysqli)); 
     if ($result) { $delete_success = true; } 
     } 

     if($delete_success) { 
     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); 

我已經確定id_array沒有得到該複選框的正確$_POST值。 vardump顯示它具有正確的上傳ID,然後我vardump id_array它顯示NULL。任何幫助將不勝感激。

CURRENTLY:

 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!"; return; }//bail if its empty 
    $delete_success = false; 
    foreach ($id_array as $id) { 
    $sql = "DELETE FROM `upload` WHERE `upload_id`=$id AND `owner_id`={$_SESSION['user_id']}"; 
    $result = $mysqli->query($sql) or die(mysqli_error($mysqli)); 
    if ($result) { $delete_success = true; } 
    } 

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

我得到正確vardump的id_array = POST之後,但在我提交刪除,該viewsource表明我正在讀(邊欄和頁腳消失)結束後,沒有什麼。目前沒有錯誤報告。

回答

1

似乎沒有任何地方給$ id_array賦值。例如,您致電的地方:

deleteUploads($id_array) 

在該點處沒有賦予$ id_array的值。

$ id_array應該有什麼值? $ _POST數組中是否存在需要分配給$ id_array的值?

而且我覺得有可能是放錯了地方「&」在這裏:

foreach ($id_array as &$id) 

我認爲它應該僅僅是:

foreach ($id_array as $id) 
+0

$ id_array應該有每個$ upload_id對應於複選框被選中,在它具有這些值之後,它逐步執行並逐個運行sql語句以刪除它們。 – V1GG3N

+0

好的 - 可能有一些代碼我看不到$ id_array的值在哪裏設置,但是除非$ id_array已被聲明爲全局變量,否則它將需要在submit_delete函數中進行本地設置。 – Dan

+0

嘗試添加print_r($ id_array)作爲deleteUploads函數的第一行,並且它會告訴您當變量到達該函數時是否包含任何值。 – Dan