Have a look at the contents of your $_POST array, in particular the names of the checkboxes that are included. If I’m reading your HTML correctly, you’ve named them using the ID field, so you should be able to iterate through them using foreach() to update your database.
I don’t see the point in your hidden variable, as that just duplicates things, by using the ID as both the array index and the value. You could do something like that to provide the original value of the checkbox, so you can see whether the database needs to be updated for each entry.
Note: If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (e.g. value=unchecked); the value is not submitted to the server at all.
Why after only changing display[3] and display[4] to Yes do all 6 records appear in the array (instead of only 2) Wouldn’t it be best to only run the UPDATE query 2 times instead of six?
If the option has no value how is Yes recorded?
Checkboxes will only post if they are selected. Which is annoying sometimes. By adding a hidden element with the same name and an unchecked value, you ensure that something is always posted. Can save you from a bunch of issets.
Though unlikely, that value could be changed client-side and cause issues. IMHO, it would be better if the same script that populates the values saves the information server-side.
Yes, but you would have to code that yourself as I mentioned earlier.
What I meant by that is to use your hidden variable to contain the original value, then when you get the $_POST array, you can check to see what has changed.
If you don’t specify a value in your <option> tag, the display string is sent through as the value. On each of your <select> tags, you have the first option that contains either No or Yes, with no specific value set, so if that selection isn’t changed, it will send the string. If these are coming from a database in the real code, it might be better to set the selected flag on the appropriate option, rather than adding another one.
In your foreach() loop to run the query, I’d have the prepare() before the loop is executed, then just call it each time. I doubt it will make any difference to perceived execution time, but one of the features of prepared statements is the ability to prepare it once and then just keep calling it with new parameters so it seems “right”.
ok, I did that, so now when the form is submitter I have two arrays (original & display). Tried the check to see if the original value was changed.
The result
Yes. Call the prepare before the loop and just pass your array in to execute in each iteration of the loop. I usually use bindParam() rather than passing an array, so I would put both of those statements before the loop, then just set the correct values within the loop and call execute.