使用sql循环将多个表单行提交到表中

时间:2017-06-08 07:41:30

标签: php sql

我从学生表中收集了多行。并列出它们就好了。

<?php

                    $sql = "SELECT * FROM `students` WHERE `class`='$class' && `academicyear`='$academicyear'";

                    $result = mysqli_query($conn, $sql);
                    if (mysqli_num_rows($result) > 0) {
                        // output data of each row
                        while($row = mysqli_fetch_assoc($result)) {

                    ?>
                                    <tr>
                                      <td><?php echo @++$n ; ?> </td>
                                      <td><?php echo $row["studentid"]?> <input type="text" name="studentid" value="<?php echo $row["studentid"]?>" style="visibility: hidden; width: 10px;"></td>
                                      <td><?php echo $row["fname"]?> &nbsp <?php echo $row["sname"]?> <input type="text" name="name" value="<?php echo $row["fname"]?>  <?php echo $row["sname"]?>" style="visibility: hidden; width: 10px;"></td>
                                      <td style="display: none; width: 10px;"><input type="text" name="class"  value=" <?php echo $class; ?>"> </td>
                                      <td style="display: none; width: 10px;"><input type="text" name="academicyear"  value=" <?php echo $academicyear; ?>"></td>
                                      <td style="display: none; width: 10px;"><input type="text" name="rowcount"  value=" <?php echo $rowcount; ?>"> </td>
                                    </tr>
                           <?php

                        }
                    } else {
                        echo "";
                    }
                    mysqli_close($conn);
                    ?> 

我的困难是如何将这多行提交到另一个名为classlist

的表中
<?php
// Receiving variables
@$pfw_ip= $_SERVER['REMOTE_ADDR'];
@$example1_length = addslashes($_POST['example1_length']);
@$studentid = addslashes($_POST['studentid']);
@$name = addslashes($_POST['name']);
@$class = addslashes($_POST['class']);
@$academicyear = addslashes($_POST['academicyear']);
@$rowcount = addslashes($_POST['rowcount']);


// Validation
//saving record to MySQL database

@$pfw_host = "localhost";
@$pfw_user = "root";
@$pfw_pw = "";
@$pfw_db = "myschool";
$pfw_link = mysql_connect($pfw_host, $pfw_user, $pfw_pw);
if (!$pfw_link) {
 die('Could not connect: ' . mysql_error());
}
 $pfw_db_selected = mysql_select_db($pfw_db, $pfw_link);
 if (!$pfw_db_selected) {
 die ('Can not use $pfw_db : ' . mysql_error());
 }

 $i=0;
 while ($i<= $rowcount) {
 @$pfw_strQuery = "INSERT INTO `classlist`(`studentid`,`name`,`class`,`academicyear`) VALUES (\"$studentid\",\"$name\",\"$class\",\"$academicyear\")" ;

 $rowcount = $rowcount - 1;
 }

 //insert new record
 $pfw_result = mysql_query($pfw_strQuery);
 if (!$pfw_result) {
 die('Invalid query: ' . mysql_error());
 }

 mysql_close($pfw_link);

 header('location:classlist.php');


?> 

我只获得提交到classlist表中的最后一行数据,然后出现错误 - 查询无效,重复查询。

请帮忙。

1 个答案:

答案 0 :(得分:0)

您必须使用单引号在SQL中包装字符串

 @$pfw_strQuery = "INSERT INTO `classlist`(`studentid`,`name`,`class`,`academicyear`) VALUES ('$studentid','$name','$class','$academicyear')" ;