• PHP Help PHP Coding
  • How can I delete several records at the same time form the database using checkbox?

Hello,

I have a table with many products, as fallow:

[checkbox] product 1
[checkbox] product 2
[checkbox] product 3
etc...

And I have "Delete Checked" botton.

How can I make this button works?

Should I give the same name to all checkboxes?

Now, when I check several product and echo the variable I get the last product only!

Many thanks...

    the checkbox name would have to be different for each product so you can tell which records to delete.

      Thanks, that's what I was thinking. But if you take a look at Yahoo! mail you'll see that all checkboxes has the same name!

        Actually, you want to use an array with the record key as the value

        <input type="checkbox" name="deletes[]" value="27" />
        <input type="checkbox" name="deletes[]" value="34" />
        <input type="checkbox" name="deletes[]" value="66" />

        Only the checkboxes which have actually been checked get posted.
        if (isset($POST['deletes']))
        {
        $deletes = $
        POST['deletes']; / Array of all keys to be deleted /
        }

        Either cycle through the array or explode it into a comma delimited list and delete away.

          18 days later

          Ok... for SOME STUPID REASON I cannot get the hang of how checkboxes work. Believe it or not I have become pretty familiar with PHP, and I am for the most part familiar with forms... but I can NOT understand WHAT to do with checkboxes once submit is pressed.

          Let's say you have a simple form, [checkbox] Selection 1, [checkbox] Selection 2, [checkbox] Selection 3

          And let's say ALL you want to do once the submit button is pressed is output to the screen the selections that had checks next to them.

          What value(s) is/are echo'd??? I can't figure it out. Probably because I've been working non-stop on a site now for QUITE a while and my patience is running low.

          If somebody could PLEASE explain this to me, I can figure out the rest of what I need to. I just get so frustrated when I get caught on a SIMPLE MATTER like this.

          Thanks!

            Let's say you have a simple form, [checkbox] Selection 1, [checkbox] Selection 2, [checkbox] Selection 3

            To create these checkboxes, here is the HTML you would use:

            <input type=checkbox value="1" name="selection_1">Selection 1<br>
            <input type=checkbox value="1" name="selection_2">Selection 2<br>
            <input type=checkbox value="1" name="selection_3">Selection 3<br>
            

            The name of the checkbox (selection_1, selection_2, selection_3) is the key used to identify that particular checkbox in the $_POST[] array.

            The value of the checkbox is the value that will get POSTed to the next page IF AND ONLY IF the checkbox is checked when the form is submitted.

            For example, if the checkbox selection_1 were checked, then the value of $POST['selection_1'] is "1". If selection_1 is not checked, then the value of $POST['selection_1'] will be nothing.

            BTW, you can use anything (within the normal limits of the <input> tag) for the value identifier (like "yes", "delete", etc.) if it helps you in code readability. I just chose to use "1" in this particular instance.

            HTH

              Write a Reply...