循环插入三次

时间:2012-02-16 11:20:50

标签: php mysql

我正在建立一个电话评估表,团队负责人会评估一个代理人,让他们给他们打电话。

这是我的代码:

$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 }}
?>

提交页面:

$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(", \n", $sql_data);
}
mysql_db_query($dbName, $sql, $connect);

我的问题是,当我提交它时,将值添加到数据库三次,它应该只插入一次。我有一个想法,它与我在另一个内部有关,但似乎无法解决问题。

我真的很感激一些帮助。

如果我回显$ 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')

谢谢

2 个答案:

答案 0 :(得分:2)

我必须说一个非常糟糕的设计。

我不知道数据是如何插入的,因为sql语句应该抛出错误。

就你的问题而言,foreach在insert语句中做了什么?您一次只能发送1个值。 Foreach每次都应该覆盖变量....

一团糟。你应该简化决定。但是,除去那个foreeach。它应该解决你的问题。

答案 1 :(得分:0)

放置代码

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

在foreach循环之外