如何从多个行和列插入记录

时间:2015-02-19 11:40:45

标签: php

Pls伙计们,我很难搞清楚这一点。我有一个页面,允许根据该班级的学生数量输入学生的记录。我不能只为列创建一个包含30个字段(test1,test2 ...)的表单,而为下一列创建另一个表单。所以我创建了一个循环。但结果并不令人愉快......而不是得到了。记录适合各自的行和列,就像这样..


Id|Name|test1|test2|exam|total|
1 | a.       |15      |0       |0        |0   
2 | a.       |0       |20      |0        |0   
3 | a.       |0       |0       |50       |0   
4 | a.       |0       |0       |0        |85   

......但我期待着这个


Id|Name|test1|test2|exam|total|
1  | a       |15     |20     |50     |85  

所以...... 我怎样才能实现这一目标

这是代码

   <?php
   if(isset($_POST['submit'])))
    {
    if(! get_magic_quotes_gpc() )
    {
    $stu_Itest = $_POST['Itest'];
    $stu_IItest = $_POST['IItest'];
    $stu_exam = $_POST['exam'];
    }
    else
    {
    $stu_Itest = $_POST['Itest'];
    $stu_IItest = $_POST['IItest'];
    $stu_exam = $_POST['exam'];
    }

    if ($_POST['Itest']) {
    //Loop through added Itest Scores
    foreach ( $_POST['Itest'] as $key=>$value ) {
    //Insert into test1_score table
    $sql_test1 = sprintf("INSERT INTO Test1_Score(Ts1_Score) VALUES ('%s')",
           mysql_real_escape_string($value) );  
    $result_test1 = mysql_query($sql_test1,$conn );
     }
     }
    if ($_POST['IItest']) {
    //Loop through added Itest Scores
     foreach ( $_POST['IItest'] as $key=>$value ) {
    //Insert into test2_score table
     $sql_test2 = sprintf("INSERT INTO Test2_Score(Ts2_Score) VALUES ('%s')",
           mysql_real_escape_string($value) );  
    $result_test2 = mysql_query($sql_test2,$conn );
    }
    }
    if ($_POST['exam']) {
    //Loop through added Exam Scores
    foreach ( $_POST['exam'] as $key=>$value ) {
    //Insert into test1_score table
    $sql_exam = sprintf("INSERT INTO Exam_Score(exam_Score) VALUES ('%s')",
           mysql_real_escape_string($value) );  
    $result_exam = mysql_query($sql_exam,$conn ));
    }
    } 

    }
    ?>

这是html代码

    <?php
    $rlt = 'SELECT id, first_name, last_name FROM students';
    $rltval = mysql_query( $rlt, $conn );
    //fetchtheresults/convertresultsintoanarray 
    define('COLS',3);//numberofcolumns
    $col=0;//jjj
    if(!$rltval)
    { 
     die('Could not get data: ' .mysql_error());
     }
     echo'<table width="100" bgcolor="#ffffff"  cellspacing="0".    cellpadding="5">
     <tr><th>ID</th><th>Name</th><th>Test1</th><th>Test2</th><th>Exam</th><th>Total</th></tr>';
      //startfirstrow
     while($rows= mysql_fetch_array($rltval, MYSQL_ASSOC))
     {$col++;
     echo"<tr><td>
      ".$rows["id"]."</td><td>
      ".$rows["first_name"]."..".$rows["last_name"]."</td><td>
      <div><input type='text' name='Itest[]' placeholder='00'></div></td><td>
      <div><input type='text' name='IItest[]' placeholder='00'></div></td><td>
      <div><input type='text' name='exam[]' placeholder='00'></div></td><td>
      <div><input type='text' name='Total[]' placeholder='00'></div></td><td>
      </td></tr>";
     if ($col=='COLS')//havefilledthelastrow
     {$col=0;
     echo'</tr><tr>';//startanewone
     }
     }
     echo'</tr>';//endlastrow
     echo"</table>";
     ?>

1 个答案:

答案 0 :(得分:0)

问题是您是为每个提交的测试插入一行新数据。您需要添加一个检查,说明用户名是否已存在,请更新该记录的测试,而不是创建新行。