Hi. I don’t write PHP and have created the following by staggering around Google for a few days, and trying to piece together the whole thing from various sources.
I have a database (tutorial) which contains a users table. The fields are: id (primary key), whenDo, name, done, and taskName. The done field contains 0 or 1 - 1 to tick the checkbox, 0 to leave it empty.
I can populate the form from the database.
BUT - I can’t get it to write changes back to the database. I don’t get any error - it simply empties any checkboxes I’ve ticked.
Second use PDO instead of mysqli as it’s easier (my opinion) and more robust as you can use different databases.
or you can save yourself a bunch of time using a 3rd party app to do the dirty work for you, by that I mean read, write, update and delete from database tables if you don’t feel like coding.
Your data definition and the output you are producing do not make sense. To record the task status per user per task, there must be one row of data for each each user for each task.
Your database should have a user(s) table, that holds the unique/one-time information about each user - id, first name, last name, username, email, hashed password, … This defines the user ids. You need a task table, that holds the unique/one-time information about each task - id, name, … This defines the the task ids. The table holding the user/task information would have columns for - id, user_id, task_id, date assigned, date due, and status. You would use a JOIN query to get the various related information from these tables, such as the user’s name (first, last), the task name, task due date, task status.
Next, the code for any page should be laid out in this general order -
initialization
post method form processing
get method business logic - get/produce data needed to display the page
html document
Since you are editing existing data, you would query for that data in item #3 on this list. However, after the form has been submitted, if there are any user/validation errors, you would want to instead use the data submitted from the form, rather than the existing data, to populate the form fields. The way to do this is to use an internal ‘working’ array variable to hold the data. You would initialize this variable to an empty array. Inside the post method form processing code, you would assign a trimmed copy of the form data to this variable. In the get method business logic, you would test if the variable is empty. If it is empty, it means that the form has never been submitted and you would query for the existing data, fetch it, and assign it to the array variable. You would use the contents of this array variable throughout the rest of the code.
You can have more than one record in a different table for the user(s) and doesn’t mean there would be more than 2 tables. The user id would point to the user in the task table with its separate id. It is like using the Dewey system in a library (I’m probably dating myself )
Your next task would be to define how you are going to display the data. Are you only going to display the incomplete tasks? Are you going to display everything, but with the complete tasks at the top? Are you going to display the tasks grouped together by name, then with the completed task first and the incomplete tasks next? Are you only going to allow incomplete tasks to be checked, making them complete, or are you going to allow previously completed tasks to be unchecked?
In your existing code, you are not associating the checkbox with the row of data it goes with. You would want to use the id as the checkbox field name’s array index. Only checked checkboxes will be set in the submitted form data. You can then test/get the ids for the checked checkboxes to use in the rest of the form processing code.