I am currently populating an array with checkboxes, imploding the array variable and using that variable in an IN clause within a DELETE function. var_dump is showing that I am creating a correct query, yet I can’t get any deletion to occur. This is a new approach to an existing problem I have had for a month or so.
Here’s what I have.
//Connect to the db
$id_array = NULL;
// Make the query to display user's uploads
$q = "SELECT upload_id, title, genre, length, created, views
FROM upload
WHERE owner_id =". $_SESSION['user_id'] ."
ORDER BY title ASC";
$r = mysqli_query ($dbc, $q); // Run the query
if($r)
{
// If it ran okay, display the records
echo '<table align="center"
cellspacing="3" cellpadding="3"
width="75%">
<tr><td align="left"><b>Title</b></td>
<td align="left"><b>Genre</b></td>
<td align="left"><b>Pages</b></td>
<td align="left"><b>Submitted</b></td>
<td align="left"><b>Views</b></td>
</tr>';
// Fetch and print all the records:
?><form action="/nbproject/newwriter_profile.php" method="post">
<?php
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
?>
the above code creates a simple table, each record is shown with a corresponding checkbox, check to delete then click “Delete”
<input type="submit" name="delete" value="Delete" align="right"/>
</form>
<?php
} // End of if ($r) IF.
mysqli_close($dbc); // Close the database connection
custom delete function:
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'];
$id_array = array_map('intval', $id_array);
$id_array = array_unique($id_array);
$id_array = implode(',',$id_array);
//var_dump($id_array);
deleteUploads($id_array);
}
else {
echo "Error: submit_delete called without valid checkbox delete.";//var_dump($_POST['checkbox']);
}
}
$delete_success = false;
function deleteUploads ($id_array) {
require_once(my connection to db);
mysqli_free_result($r);
if (count($id_array) <= 0) { echo "Error: No deletes in id_array!"; echo 'wtf'; }
//var_dump($id_array);
above var dump shows correct id’s as shown in my mysql database
$gone = "DELETE FROM `upload` WHERE `upload_id` IN ("."$id_array".")";
var_dump($gone);
above var verifies that the query is formed correctly, with comma separated id’s
$goodbye = mysqli_query($dbc, $gone);
if ($goodbye) { $delete_success = true; var_dump($delete_success);}
else { $delete_success = false;}
var_dump($delete_success);
above var dump shows bool false
//mysqli_close($dbc);
if($delete_success == true) { echo 'done';
header('Location: newwriter_profile.php');
} else
{
echo "Error b/c delete failed";
the above echo shows after checking any number of boxes and hitting delete
}
}
submit_delete();
mysqli_free_result($goodbye);
mysqli_close($dbc);
?>
I solved my problem. I realized that during my haphazard development, I had created my database user with associated privileges BEFORE actually creating my uploads table. I ran a grant permission statement on my database user and miraculously I am able to delete using the above code. This also solved another issue I was having involving SESSION variables getting lost between pages.
This was an absolute nightmare – I hope this helps someone else as well. Thanks for your input.