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?
Here’s two parts of my code. First part below is my JavaScript - showing what I am appending into the document. This is also how I make it dynamic, using ajax as well
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.
//PART #1........javascript - adding the dynamic options
$(document).on('click', '.addgoalies', function(){ //when user clicks 'add', all this below will be added to html
count++; //counter is added by 1 each time 'add' is clicked
var html = '';
html += '<div class="goal"><select name="teamGoalie[]" class="teamzGoalie" data-playerz="'+count+'" ><option value="">Team</option><?php echo fill_select_box($pdo, "0"); ?></select>';
html += '<select class="goal" name="playerGoalie[]" id="playermenu'+count+'"><option value="">Player</option></select>';
html += '<input type="number" name="goalsAgainst[]" placeholder="0" class="tendy" id="goaliemenu'+count+'">';
html += '<input type="radio" name="statusGoalie[]" value="finish" class="status" id="goaliemenufinish'+count+'"><label for "finish">FINISH</label>';
html += '<button type="button" name="remove" class="remove"><span class="glyphicon glyphicon-minus">REMOVE</span></button></div>';
$('.wrapper3').append(html);
});
//PART #2........the working php query, and then the one i'm stuck on
<?php
$goalieresult = $_POST['statusGoalie'];
$playerGoalie = $_POST['playerGoalie'];
$goalsagainst = $_POST['goalsAgainst'];
for($count = 0; $count < count($playerGoalie); $count++){
$data1 = array(
':goalsagainst'=>$goalsagainst[$count],
':playerid'=>$playerGoalie[$count]
);
$sql1 = "UPDATE goalies SET
goals_ag = goals_ag+:goalsagainst
WHERE id = :playerid";
$statement1 = $pdo->prepare($sql1);
$statement1->execute($data1); //execute statement
}
//////////////////////////////////////////////////////////////
for($count = 0; $count < count($playerGoalie); $count++){
$data2 = array(
':playerid'=>$playerGoalie[$count]
);
if($goalieresult[$count] == 'finish'){
$sql2 = "UPDATE goalies SET
Wins = Wins+1
WHERE id = :playerid";
$statement2 = $pdo->prepare($sql2);
$statement2->execute($data2); //execute statement
}
}
?>