First post here. Just found this place - happy to be apart of it.
TLDR: My data is inserting to wrong columns in database. Has to do with my count feature not working properly for a radio input. So when user submits form, the data is being inserted for the wrong players. Read below for more in depth detail. My code is at the end. Tried asking this on stack overflow and it was closed due to it being “duplicate”. You can find it here - i couldnt get anything of value though: https://stackoverflow.com/questions/35856766/php-inserting-multiple-checkbox-and-textbox-arrays-into-mysql-database
I have a database that stores a bunch of stats and players for a sports league. Pretty basic.
I have a form that allows users to submit all the game data that goes into the database.
In the form is a section where I allow users to dynamically add goal scorers. For each goal scored, they click on an “add” button and it adds a whole line. These dynamic options all share the same name, count as their own individual records.
It works fine with select options, and inputs (integers), however when I add an input type radio, the functionality only half works (for radio option only).
For example, when I click “add goalie”, the dynamic row pops up. I then select team, player and type in (integer value) how many goals he let in.
I can add as many goalies as I wish, and after submitting, all their goals against will stick to their designated goalie. Meaning, if I had goalie A letting in 2 goals, and goalie B letting in 3 goals, that’s how it will insert into my database. Hope that makes sense. Basically all unique rows and my form understands that.
The problem is when I add a fourth option as an input with type radio. This is just one radio that the user has to select, for whomever finished the game. So out of all the goalies played, only one will finish the game since it’s a radio not a checkbox. The one who finishes, gets credited with either a win or a loss. The rest don’t get anything.
I applied my same technique by putting my query into a for loop, but what happens with my radio is only the first goalie gets credited with anything. If I had four goalies and selected “finished” on the last one, it would apply the radio stats to the first goalie instead.
I think I know my problem and it has to do with the count for my radio. The reason why the input before it for goals against worked, was because since it was a number I could put it into an array to call in my SQL1. Called it “:goalsagainst” (See $data1).
But with radio it’s not an integer so I didn’t know how to add it into my data array. So I left it out of the data array, and tried adding the $count in my if statement instead.
This got me close but still off since it’s crediting the wrong player.
Can anyone help me out? Still learning so this was best way I could explain , with walls and walls of text. Is there a better way I can use my if statement, or maybe somehow combine the if statement with the for loop it’s within?
Then the second part, showing my PHP. Showing two queries here: First one as an example of what works with my input integer option. And then the second snippet is showing the code I’m stuck on - for the forth radio input. Thank you in advance, hope all this helped and wasn’t too complicating. Much appreciated.