Good afternoon StackOverflowers,
I've got a pretty simple question. (I guess:))
Everytime I submit a form with many other input fields it takes the last value of a input field.
I had a similar problem a month ago, but I fixed it somehow.. I just can't fix this problem..
Below you can see my HTML form. (No markup, I know)
<?php
include_once("database.php");
$sql = "SELECT * FROM statements";
$stmt = $db->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
echo "<b>";
echo $row['question'];
echo "<br/></b>";
$sqlA = "SELECT * FROM question_answer WHERE question_id =" . $row['id'];
$stmtA = $db->prepare($sqlA);
$stmtA->execute();
$rowsA = $stmtA->fetchAll(PDO::FETCH_ASSOC);
echo "<form id='modify' name='modify' action='modify.php' method='POST'>
<div id='answers'>";
foreach($rowsA as $rowa){
if($rowa['correct_answer'] == 1){
$rowAnswer = $rowa['answer'];
$rowId = $rowa['question_id'];
echo "<input type='text' checked value='" . $rowAnswer . "' name='" . $rowId."' style='background:lightgreen;'><br/>";
echo "</div>";
}
else{
$rowFalseId = $rowa['question_id'];
echo "<input type='text' value='" . $rowAnswer . "' name='" . $rowFalseId."'><br/>";
}
}
}
?>
<input type='submit' name='modify_answers' value="Modify Answers">
</form>
Below you can see my Update Query;
<?php
include_once("database.php");
// foreach($_POST as $val){
// print_r($val);
// }
$sql = "SELECT * FROM statements";
$stmt = $db->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
$sqlA = "SELECT * FROM question_answer where question_id =" . $row['id'];
$stmtA = $db->prepare($sqlA);
$stmtA->execute();
$rowsA = $stmtA->fetchAll(PDO::FETCH_ASSOC);
foreach($rowsA as $rowa){
foreach($_POST as $id => $value){
$update = "UPDATE question_answer SET answer = '".$value."' WHERE question_id ='". $id."'";
$stmt = $db->prepare($update);
$stmt->execute();
}
}
}
?>
Everytime It submits to the modify.php file it updates the last value of a input field.
In example if I have 16 input fields, it takes the 4th, 8th, 12th and 16th value of the input field.
So the problem is probaly with the name of the input field, but it could be also the modify.php..
Any help is appericiated! I'm struggling days with this easy problem:(
Thanks in Advance Guys!!