Reputation: 309
I'm trying to get multiple deletion capability, and this is what I have so far:
<?php
echo '<h3>Welcome to your profile, '. $_SESSION['first_name'] . '</h3>';
require_once ('../mysqli_connect.php'); //Connect to the db
// Make the query
$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) {
?>
<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>';
<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">'
. '<input type="checkbox" name="checkbox[]"'
. 'id="checkbox[]" value='.$row['$upload_id'] //this upload id is auto-increment (see first note)
.' />'.' </td>'. '</tr>';
}
?>
</table>
<input type="submit" name="delete" value="Delete" align="right"/>
</form>
<?php
mysqli_free_result ($r); // Free up the resources
} else { // If it did not run okay
// Public Message:
echo '<p class="error">Your submissions could not be retrieved. We apologize for any inconvenience.</p>';
// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
} // End of if ($r) IF.
mysqli_close($dbc); // Close the database connection
if($_POST['delete']) {
require_once ('../mysqli_connect.php'); //Connect to the db
$checkbox = $_POST['checkbox']; //from name="checkbox[]"
$countCheck = count($_POST['checkbox']);
for($i=0;$i<$countCheck;$i++) {
$del_id = $checkbox[$i];
//----------------------------------
$sql = "DELETE from `upload` where `upload_id` = $del_id AND `owner_id` = {$_SESSION['user_id']}";
$result = $mysqli->query($sql) or die(mysqli_error($mysqli)); //added session information after hearing about google spider armageddon scenarios
}
if($result) {
header('Location: newwriter_profile.php');
} else {
echo "Error: ".mysqli_error();
}
}
?>
1st Note: Basically the registered user is greeted by their profile page which shows them their uploads and some upload info. I'd like a checkbox alongside each upload and a delete button wherever else on the page. So I tried this.
Right now it will redirect with no errors and nothing is deleted. Also, hitting Delete without a checkbox selected returns an error. Thanks for the help in advance, solving this issue will also solve another I am having and will be a huge relief.
I can't think of a way to get the upload id to be linked to the checkbox. My brain hurts.
EDIT: This is the current output of the viewsource for the checkbox, using dcoder's recommendation and the vardump test. input type="checkbox" name="checkbox[]" id="checkbox[]" value= "" />
vardump test output: array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" }
EDIT: delete functions are currently as follows:
function submit_delete() {
if($_POST['delete'] && $_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($_POST['checkbox[]']);
}
}
function deleteUploads ($id_array) {
if (count($id_array) <= 0) { 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);
EDIT: Currently is as follows:
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[]"'. 'id="checkbox[]" value= "'.$row['upload_id'].'"'.' />'.' </td>'. '</tr>';
commented out vardump, shows that each checkbox is getting the correct upload_id...
function submit_delete() {
if(!is_null($_POST['delete']) && !is_null($_POST['checkbox[]'])) { //Check to see if 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($_POST['checkbox[]']);
}
else {
echo "Error: submit_delete called without valid checkbox delete.";
}
}
function deleteUploads ($id_array) {
if (count($id_array) <= 0) { echo "Error: No deletes in id_array!"; var_dump($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();
}
}
//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);
on page load, I get the error "submit_delete called..." after I tick some boxes and hit delete... I get the same error. Bailing out on the $_POST['checkbox[]'] and getting a NULL value.
Upvotes: 1
Views: 1607
Reputation: 309
I solved the problem. I ran a grant * permission statement in MySQL after exhausting every other option. Apparently I had created the database user prior to creating the Uploads table. Thanks all for the help
Upvotes: 0
Reputation: 21230
I've broken your functionality out into functions you can call. The former function will help you test that you're getting the right values. You can manually populate an array with values you know are in the database to check the latter function.
function submit_delete() {
if($_POST['delete'] && $_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($_POST['checkbox']);
} else {
echo "Error: submit_delete called without valid checkbox delete.";
}
}
function deleteUploads ($id_array) {
if (count($id_array) <= 0) { //bail if its empty
echo "Error: No deletes in id_array!";
return;
}
$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[] = 10;//make sure this id is in the db
$test_ids[] = 9;//this one too
deleteUploads($test_ids);//should remove ids 10 and 9
Upvotes: 0
Reputation:
The problem is here:
'<input type="checkbox" name="checkbox[]"'
. 'id="checkbox[]" value='.$row['$upload_id']
.' />'
Change this to the following:
'<input type="checkbox" name="checkbox[]"'
. 'id="checkbox[]" value="'.$row['upload_id'] . '"'
.' />'
You're missing quotes("
) around the value, and you're outputting the wrong index.
Also, please use bind variables for embedding dynamic elements into SQL queries, and escape dynamic input before outputting it to the page.
Upvotes: 1