循环插入三次

I'm building a call evaluation form, team leaders evaluate a agent to give them a score on their calls.

Here is my code:

$query = "SELECT uid, section_name, section FROM sections_phase1 WHERE client_uid_fk = '$client'";
$sql = mysql_db_query($dbName,$query,$connect);// I'm getting section1,2,3 here

while ($sec_row = mysql_fetch_assoc($sql)) {

    ?>
<tr>
    <td class="style18"><?php echo $sec_row['section_name']?></td>
</tr>
<?php
$countquestion = 0;
$query = "SELECT uid, question FROM evaluation_phase1 WHERE client_uid_fk = '$client' AND section = '{$sec_row['section']}'";
$result = mysql_db_query($dbName,$query,$connect);

while ($row = mysql_fetch_assoc($result)) {
    $countquestion++;
    ?>


<tr>
    <td class="style8"><?php echo $row['question']?><input
        type="hidden" name="question_id_p1[<?php echo $row['uid']?>]"
        value="<?php echo $row['uid']?>" /> <input type="hidden"
        name="section[]" value="<?php echo $sec_row['section']?>" /></td>
    <td>
    1<input type="radio" name="answer_p1[<?php echo $row['uid']?>]" value="1" />                         
</td>
    <td>
    2<input type="radio" name="answer_p1[<?php echo $row['uid']?>]" value="2" />
</td>
    <td>
    3<input type="radio" name="answer_p1[<?php echo $row['uid']?>]" value="3" />
</td>
    <td>
    4<input type="radio" name="answer_p1[<?php echo $row['uid']?>]" value="4" />
</td>
    <td>
    5<input type="radio" name="answer_p1[<?php echo $row['uid']?>]" value="5" />
</td>

    <td>
    N/A<input type="radio" name="answer_p1[<?php echo $row['uid']?>]"
        value="0" />
</td>

    <td>
    <textarea rows="1" cols="15" name="comment_p1[<?php echo $row['uid']?>]"></textarea>
</td>
</tr>
<?php }}
?>

Submit Page:

$sql_data = array();
$sql_prefix = "INSERT INTO agents_phase1(call_info_uid_fk, client_uid_fk, product_uid_fk, team_leaders_uid_fk, agent_uid_fk, question_id, question_answer, section, evaluation_number, comment, time) VALUES";
foreach($_POST['answer_p1'] as $id => $answer){
    $call_info_id = (int) $_POST['call_id'];
    $client_id = (int) $_POST['client_id'];
    $product_id = (int) $_POST['product_id'];
    $team_leader_id = (int) $_POST['team_leader_id'];
    $agent_id  = (int) $_POST['agent_id'];
    $question_id  = (int) $_POST['question_id_p1'][$id];
    $answer      = (int)($answer);
    $comment      = mysql_real_escape_string ($_POST['comment_p1'][$id]);
    $used_time = $timeend-$timestart;
    $section = (int) $_POST['section'][$id];

    $sql_data[] = "('$call_info_id', $client_id, $product_id, $team_leader_id, $agent_id, $question_id, '$answer', '$section', '1', '$comment', '$used_time')";

    $sql = $sql_prefix.implode(", 
", $sql_data);
}
mysql_db_query($dbName, $sql, $connect);

My problem is when I submit it adds the values to database three time, where it should only insert once. I have an idea that it has something to do with my while inside another while, but can't seem to get the problem.

I would really appreciate some help.

This is the output if I echo $sql:

INSERT INTO agents_phase1(call_info_uid_fk, client_uid_fk, product_uid_fk, team_leaders_uid_fk, agent_uid_fk, question_id, question_answer, section, evaluation_number, comment, time) VALUES('8', 9, 7, 8, 24, 1, '3', '1', '1', '', '704197'), ('8', 9, 7, 8, 24, 2, '4', '1', '1', '', '704197'), ('8', 9, 7, 8, 24, 3, '2', '1', '1', '', '704197'), ('8', 9, 7, 8, 24, 4, '4', '1', '1', '', '704197'), ('8', 9, 7, 8, 24, 5, '2', '1', '1', '', '704197'), ('8', 9, 7, 8, 24, 22, '4', '1', '1', '', '704197'), ('8', 9, 7, 8, 24, 6, '5', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 7, '3', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 8, '4', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 9, '3', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 10, '4', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 11, '3', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 12, '4', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 13, '3', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 14, '4', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 15, '3', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 16, '4', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 17, '3', '2', '1', '', '704197'), ('8', 9, 7, 8, 24, 18, '4', '2', '1', '', '704197')

Thanks

A very very bad design i must say.

I don't know how the data gets insert because that sql statement should throw an error.

As far your problem, what is foreach doing on insert statement? You can send only 1 value at a time. Foreach should overwrite the variables each time....

A big mess. You should simplify the decision. But none the less, remove that foreeach. It should resolve your problem.

Put the code

$sql = $sql_prefix.implode(", 
", $sql_data);

outside the foreach loop