0

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!!

Taykklan
  • 45
  • 6
  • Just print the query instead of executing,, You yourself can able to figure it out the problem – Sulthan Allaudeen Jun 03 '15 at 12:28
  • Tried Many times without luck :( I'm struggling, but It seems to be a simple problem.. But it is still not fixed:( – Taykklan Jun 03 '15 at 12:29
  • Run away from loop updating. Instead of updating in a loop [check out multiple updates in one query](http://stackoverflow.com/questions/20255138/sql-update-multiple-records-in-one-query). That will probably solve you're current problem. Also you're highly vulnerable to sql injections. – Andrei Jun 03 '15 at 12:29
  • @andrew Thanks for that! Thats really helpful, better than doing single queries :) – Taykklan Jun 03 '15 at 12:51

2 Answers2

2

I think I can see what's happening here. You need to move this line...

$rowAnswer = $rowa['answer']; 

... so it doesn't fall within your if statement (move them up above it). It's only been assigned, currently, if $rowa['correct_answer'] == 1 evaluates to true.

d0ug7a5
  • 692
  • 4
  • 7
  • Nope, without luck :( It is still the same result.. Still taking the last value of the input field. – Taykklan Jun 03 '15 at 12:32
  • NB It still needs to be within the foreach loop, just not within the if, but I guess you've done that. – d0ug7a5 Jun 03 '15 at 12:34
  • It is fixed Guys, I had the $rowa['question_id'] which had everytime the same number.. That was the problem, I should get the id instead.. But anyways thanks for the help!! – Taykklan Jun 03 '15 at 12:38
1

It is fixed guys! Thanks for your help.. @d0ug7a5 thanks for the answer, that helped me. But my main problem was that I was getting the Question_id instead of the id. Thanks!!

Taykklan
  • 45
  • 6