Hello there,
I’m looking forward for a way on updating multi rows in my database.
I have a table with many fields and 4 of them are ENUM fields with values YES or NO (IsActive, Index, Hot, New). If the value of "IsActive=YES’ the row is active (Index=is showin in index; hot=is showing a hot image; new=is showing a new image). When i list all the rows of that db, for those field i have made a checkbox that is checked if the value is YES and unchecked if the value is NO. What i need to do now, is a way to modify multi rows of db only the field affected by checking or unchecking option and pressing a submit button.
When i press the submit button, will be updated only rows that has any changes on it and other rows will remain intact.
I have attached an print screen of my page to gett a better idea on what i want to do.
The first thing you need to do is make sure all the checkboxes for each ID in your form have the same name. Then each ID will send an array of checked checkboxes.
The name should include the ID value to make it easier in the php script to see which ID the checkbox values belong to.
Your php script will then receive an array of checked checkbox values for each ID.
You then loop thorough each set of checkboxes for each ID and build your update query based on the values of the checkboxes for that ID. The value of each checkbox will tell you which columns you need to include in your update query.
After you build an update query for each ID, then run it to update the selected columns for that ID.
Where value=ID of the row. In this case, the row have ID 2
I have changed the type of field for this fields to TINYINT with values 1 and 0 from ENUM.
The tablename is paketa.
Now, having all this details, can you help me to build that query and function, PLEASE?
You said that i should use name=chkBox_1 for each one, but those 4 field are 4 diferent field and have their diferent field on db too.
Also, value=“val_1” where val_1 should be what?
…where name=FielName_RowID and Value=FielRecord, 0 or 1. Where the value is 0 the checkbox is blank, and at value = 1 the checkbox is ticked.
Hope that since here i’m doing well. Now, how to build the update query? can you help me on this too, please?
did you read the coding and debugging link I posted earlier?
in it I suggest the first thing you need to do in your php script is make sure the form data is coming across correctly. if it isn’t, there is no point in coding anything else until the form data comes across correctly.
now that you have changed the names in your form, have you checked the values of the checkbox are being received correctly in the php script?
Yep, i have checked and they are working correctly. In the first post i had a mistake in coding since i give the same name to all four fields and that wasn’t correctly since every field has it’s own name and it’s own field in db.
the naming system you used will probably still be ok but imho will make your php code trickier.
I would have done it this way.
Going back to your image of checkboxes in post 1, I would have given the 4 checkboxes for each ID the same name and the value of each checkbox for an id set to the column name it represents in the database. This way, the array of checked check boxes for each id will contain the column names you want to update.
and then similarly for each 4 checkboxes for the other ID’s
Then you can loop through each array of checkboxes to see which column names were checked and you can then build your update query with those column names.
ok then it looks like you’re probably a little ahead of yourself taking on a project like this which requires you to know at least the basics of php and sql.
perhaps, you would better off at this stage to first get a good book on php and sql and work through that learning the basics. Basically learn to walk with php before you start to run with it.
short of actually writing the code for you there probably isn’t much more I can do at this stage.
so out of curiosity, why are you taking on a project of this size rather than a smaller project since you are new to php?
I’m learning and reading tutorials but, for this type of process does not exists many tutorials. This is not a big project but a little one and a basic one. To work with check-boxes is the most hard work/process on my project. I was and I’m just learning and i don’t like theory … I’m learning evrything with examples and practice.
In this case, if you can write the code, i’ll be able to understand and continue it.
Anyway, thank you for your help. It was very helpful for me.
Thanks!
It’ll take less time for me to help you fix the code you come up with from the tutorials you are doing than to put the code together for you and simply post it.
since you like learning from examples and practice, why not have a go at writing the code yourself and if you get stuck post your code and we can try to help.
the concepts I posted earlier in the coding and debugging link might help in your general work flow.
I still think SQL/mySql should do the work here. Kalon is absolutely right all select boxes should have the same name. (lets call them status) Since the rows you want to update are already in the database (otherwise you would not talk about an update) you can use a hidden holding the ID (id) for each row. Your update query would than look like:
UPDATE
your_table
SET
status = 1
WHERE
id IN ( #Form.id# )