Forum Moderators: coopster

Message Too Old, No Replies

Will these checkboxes delete all profiles checked?

         

jbearnolimits

1:12 am on Oct 24, 2022 (gmt 0)

Top Contributors Of The Month



Thanks to everyone that has helped me thus far. I've been able to setup a contact database with the ability to search, filter, sort, add tasks, and more. There's one more thing I need some help with though.

I'm now attempting to include checkboxes in the rows of records because I want to be able to do bulk updates. I'd like to click the top button to select all others and choose to add a campaign or delete multiple profiles and so on. But I'm not sure how its going to work as each checkbox will pass a different id to the update script.

What I have so far is the code for the form.

<div class="BulkForm">

<form action="../Bulk.php" method="post">

<select id="Type" name="Type">
<option value="Action">Action</option>
<option value="AddAutopilot">Add Autopilot</option>
<option value="RemoveAutopilot">Remove Autopilot</option>
<option value="Delete">Delete</option>
<option value="ModifType">Modify Type</option>
<option value="Export">Export To CSV</option>
</select>

</div>

<?php
$sql = "SELECT * FROM Contacts ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row["id"];
$FirstName = $row["FirstName"];
$LastName = $row["LastName"];
$Email = $row["Email"];
$Phone = $row["Phone"];
$HomeAddress = $row["HomeAddress"];
$CreationDate = $row["CreationDate"];
?>

<div class="BulkSubmit"><input type="submit" value="Submit"></div>

<div class="InnerDatabaseTableTopRow">
<div class="InnerDatabaseTableTopRowProfilID"></div>
<div class="InnerDatabaseTableTopRowName">NAME</div>
<div class="InnerDatabaseTableTopRowEmail">EMAIL</div>
<div class="InnerDatabaseTableTopRowPhone">PHONE</div>
<div class="InnerDatabaseTableTopRowHomeAddress">ADDRESS</div>
<div class="InnerDatabaseTableTopRowCreationDate">CREATION DATE<div class="InnerCreationDateCheckBoxTop"><input type="checkbox" id="BulkCheckbox" name="BulkCheckbox" value="<?php echo "$id";?>"></div></div>


<div class="OuterDatabaseRowType1">
<div class="InnerDatabaseRowType1ProfilID"><ul class="second"><li class="second"><a href="../Profile.php/?id=<?php echo "$id";?>">View Profile</a></li></ul></div>
<div class="InnerDatabaseRowType1Name"><?php echo "$FirstName $LastName";?></div>
<div class="InnerDatabaseRowType1Email"><?php echo "$Email";?></div>
<div class="InnerDatabaseRowType1Phone"><?php echo "$Phone";?></div>
<div class="InnerDatabaseRowType1HomeAddress"><?php echo "$HomeAddress";?></div>
<div class="InnerDatabaseRowType1CreationDate"><?php echo "$CreationDate";?> <div class="InnerCreationDateCheckBox"><input type="checkbox" id="BulkCheckbox" name="BulkCheckbox" value="<?php echo "$id";?>"></div></div>
</div>

</form>


What I was thinking about doing on the Bulk.php page is the following sql query:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$id = $_POST['id'];
$Type = $_POST['Type'];
}

if (Type = "Delete") {
$sql = "DELETE FROM `Contacts` WHERE `id` = $id";

if ($conn->query($sql) === TRUE) {

}

$sql = "DELETE FROM `Tasks` WHERE `UserID` = $id";

if ($conn->query($sql) === TRUE) {
?><h2>Profile Removed</h2>
<?php };?>


But I'm not sure if this will work to delete all profiles with a checkbox clicked or not. I am assuming the form will post the id's to the Bulk.php page, but then will this query be able to delete all of the ones clicked on?

vincevincevince

7:08 pm on Nov 2, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Name your checkbox e.g. name="BulkCheckbox[]"
Then you read the list of id values from $_POST['id'] as an array, e.g.
$ids=$_POST['id'];
foreach ($ids as $id)
{
$id=intval($id); //make sure nobody is injecting things into your SQL by forcing it to be an integer
$sql = "DELETE FROM `Contacts` WHERE `id` = $id";
$conn->query($sql);
}


ps I really don't understand what you are trying to do with
$sql = "DELETE FROM `Tasks` WHERE `UserID` = $id";

But if it should have the same ID numbers as the multiple checkbox, it goes into the foreach loop.

jbearnolimits

3:41 am on Nov 18, 2022 (gmt 0)

Top Contributors Of The Month



I seem to be confused here. I named the checkbox BulkCheckbox[] and put a value of value="<?php echo "id"; ?> in as you can see at the last couple of lines of the code box here (which I am showing everything other than style and database information so I don't lack any important information to give you).

<div class="OuterBulkBody">
<div class="BulkWords">Bulk Actions:</div>
<div class="BulkForm"><form action="../BulkDelete.php" method="post">
<select id="Type" name="Type"> <option value="Action">Action</option>
<option value="AddAutopilot">Add Autopilot</option>
<option value="RemoveAutopilot">Remove Autopilot</option>
<option value="Delete">Delete</option>
<option value="ModifType">Modify Type</option>
<option value="Export">Export To CSV</option>
</select>
</div>
<div class="BulkSubmit"><input type="submit" value="Submit"></div>
</div>
<div class="OuterSearchBody">
<div class="SearchWords">Search:</div>
<div class="SearchForm"><form action="../Search.php" method="post">
<input type="text" id="Search" name="Search">
</div>
<div class="SearchSubmit"><input type="submit" value="Search"></form></div>
</div>
<div class="TopDatabaseTable"></div>
<div class="OuterDatabaseTableTopRow">
<div class="InnerDatabaseTableTopRow">
<div class="InnerDatabaseTableTopRowProfilID"></div>
<div class="InnerDatabaseTableTopRowName">NAME</div>
<div class="InnerDatabaseTableTopRowEmail">EMAIL</div>
<div class="InnerDatabaseTableTopRowPhone">PHONE</div>
<div class="InnerDatabaseTableTopRowHomeAddress">ADDRESS</div>
<div class="InnerDatabaseTableTopRowCreationDate">CREATION DATE <div class="InnerCreationDateCheckBoxTop"><input type="checkbox" id="BulkCheckbox" name="BulkCheckbox" value="BulkCheckbox"></div></div>

<?php
$sql = "SELECT * FROM Contacts ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row["id"];
$FirstName = $row["FirstName"];
$LastName = $row["LastName"];
$Email = $row["Email"];
$Phone = $row["Phone"];
$HomeAddress = $row["HomeAddress"];
$CreationDate = $row["CreationDate"];
?>
<div class="OuterDatabaseRowType1">
<div class="InnerDatabaseRowType1ProfilID"><ul class="second"><li class="second"><a href="../Profile.php/?id=<?php echo "$id";?>">View Profile</a></li></ul></div>
<div class="InnerDatabaseRowType1Name"><?php echo "$FirstName $LastName";?></div>
<div class="InnerDatabaseRowType1Email"><?php echo "$Email";?></div>
<div class="InnerDatabaseRowType1Phone"><?php echo "$Phone";?></div>
<div class="InnerDatabaseRowType1HomeAddress"><?php echo "$HomeAddress";?></div>
<div class="InnerDatabaseRowType1CreationDate"><?php echo "$CreationDate";?> <div class="InnerCreationDateCheckBox"><input type="checkbox" id="BulkCheckbox[]" name="BulkCheckbox[]" value="<?php echo "id"; ?>"></form></div></div>
</div>
}


In the BulkDelete page I have what is in the following code box. Oh, and the reason I have $UserID = $id is because the Contacts table has the id# of the person, but the Tasks table has a column with id# for the task id. So I included a column in Tasks called UserID to hold the id number of the person from Contacts.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$UserID = $_POST['id'];
$ids= $_POST['id'];

foreach ($ids as $id)
{
$id=intval($id); //make sure nobody is injecting things into your SQL by forcing it to be an integer
$sql = "DELETE FROM `Contacts` WHERE `id` = $id";
$conn->query($sql);
}

foreach ($ids as $id)
{
$id=intval($id); //make sure nobody is injecting things into your SQL by forcing it to be an integer
$sql = "DELETE FROM `Tasks` WHERE `UserID` = $id";
$conn->query($sql);
}
if ($conn->query($sql) === TRUE) {
?>

<h2>Profiles Removed</h2>

<div class="ProfileRowsTop">

Profiles Removed.<br<br>REDIRECTING IN 1 SECONDS

</div>

}


Right now I am just getting a lot of errors on the BulkDelete page saying:

Notice: Undefined index: id in ../BulkDelete.php on line 15

Notice: Undefined index: id in ../BulkDelete.php on line 16

Warning: Invalid argument supplied for foreach() in ../BulkDelete.php on line 18

Warning: Invalid argument supplied for foreach() in ../BulkDelete.php on line 26

Notice: Undefined variable: sql in ../BulkDelete.php on line 32

Warning: mysqli::query(): Empty query in ../BulkDelete.php on line 32

Notice: Undefined variable: sql in ../BulkDelete.php on line 53
Error:

What am I doing wrong?