dtslobe4694 2012-02-16 11:20
浏览 97

循环插入三次

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

  • 写回答

2条回答 默认 最新

  • douquanqiao6788 2012-02-16 11:45
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?