Jump to content

Drummin

Members
  • Posts

    1,004
  • Joined

  • Last visited

Community Answers

  1. Drummin's post in Can't execute php to delete multiple rows in database was marked as the answer   
    Yes, as Barand pointed out you need to move the form tags out of the while loop so they also contain the submit button and from what I see they might as well go out to the body tags so there are no nesting issues with the <div> tags.  Another Big issue is that you are not echoing the ID in the checkbox input.  I would also remove the id=".delete-checkbox" attribute. It is not needed and as was mentioned id's needs to be unique.
    Now you can't just assign the checkbox value to a variable because you are dealing with an POST array.  To SEE what is being sent you can print the post like so, which will give you a good picture of what you are dealing with.
    echo "<pre>"; print_r($_POST); echo "</pre>"; Now testing looks like this.
    Array ( [delete] => MASS DELETE [delete-product-btn] => Array ( [0] => 3 [1] => 4 [2] => 6 [3] => 7 ) ) these array ids need to be converted into a comma separated string like 3,4,6,7 to be used in your IN() condition.  Now a person could just implode this array defining the separator as a comma.
    $ids = implode(',', $_POST['delete-product-btn']); However it's a good practice to bind values being sent to a query. To do this those 3,4,6,7 values would need to be replaced with question mark placeholders.  You can make an array of question marks using array_fill() which says "starting with 0 as the first KEY repeat 4 times placing a value as a question mark while making the array. But to make this dynamic we will use count($_POST['delete-product-btn']) in place of 4.
    array_fill(0, count($_POST['delete-product-btn']), '?') Which looks like this when printed.
    Array ( [5] => ? [6] => ? [7] => ? [8] => ? ) ..then implode with the comma.
    $placeholders = implode(',', array_fill(0, count($_POST['delete-product-btn']), '?')); We need define the Type of values we are dealing with for each value being passed.  In this case integers, which are defined as i .  So we are looking for iiii, which can be made with str_repeat() again using count to defined how many i's we need.
    $bindString = str_repeat("i",count($_POST['delete-product-btn'])); You can now prepare, bind and execute the query like so.
    $sqlDelete = "DELETE FROM test WHERE id IN($placeholders)"; $queryDelete = $conn->prepare($sqlDelete); $queryDelete->bind_param($bindString, ...$_POST['delete-product-btn']); $queryDelete->execute(); If you wanted to know the number of records that were affected you could add this line right after the query.
    $num = mysqli_stmt_affected_rows($queryDelete); I would wrap all your processing code in an IF condition so it is not processed if no checkboxes are checked.
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete']) && !empty($_POST['delete-product-btn'])): $placeholders = implode(',', array_fill(0, count($_POST['delete-product-btn']), '?')); $bindString = str_repeat("i",count($_POST['delete-product-btn'])); $sqlDelete = "DELETE FROM test WHERE id IN($placeholders)"; $queryDelete = $conn->prepare($sqlDelete); $queryDelete->bind_param($bindString, ...$_POST['delete-product-btn']); $queryDelete->execute(); $num = mysqli_stmt_affected_rows($queryDelete); if(!empty($num)){ $plural = ($num !== 1 ? 's' : ''); $verb = ($num !== 1 ? 'Were' : 'Was'); echo $num.' Record'.$plural.' '.$verb.' Deleted Successfully.'; } endif;  
  2. Drummin's post in php mysql deleting a record was marked as the answer   
    As I understand it in the `users` table the users ID is in the field `id`, which is pretty common but the user may have records in the `bandstage` table and the `id` field represents the bandstage record ID and user ID is held in the `mybandid` field. This seemed to be what you were saying with your table image.  So to delete the user records you query against the field that represents the user ID, which in this table is `mybandid`. 
    $sql = "DELETE FROM `bandstage` WHERE `mybandid` = '$mybandid'";  
     
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.