到目前为止,我所拥有的是(我认为是)一个工作循环,迭代并获得正确的 id(由 var_dump 确认),但我的 sql 查询没有将 id 作为 DELETE 键。本质上,SQL 查询是删除 id = 数组迭代的当前 id 值的地方。使用多个复选框。var dump 正在确认 id 与上传 id 匹配,但是我无法进行任何删除。这是代码:
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);