What I have so far is (what I believe to be) a working loop, iterating through and getting the correct id's (confirmed by var_dump), yet my sql query is not picking up the id's as the DELETE keys. Essentially, SQL query is deleting where the id = the current id value of the array iteration. Uses multi checkbox. var dump is confirming that the id matches the upload id, however I can't get any deletion to take place. Here's the code:
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);
You need to remove the mysqli_close($dbc);
statement from your deleteUploads()
function.
I also agree yhou should work on accepting peoples answers. You've asked 9 questions and you haven't accepted one of the 13 answers. This isn't really playing fair.
Edit I've quickly run through your script and moved it around a bit. Does this provide any useful information?
<?
$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(!@mysqli_close($dbc))
{
$msgs[] = 'Error: mysqli_close failed';
}
echo implode('<br>',$msgs);
?>