Passing multiple rows to table using sql loop

I have collected a few rows from the student table. and its listing them is just fine.

<?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);
                    ?> 

      

my difficulty is how to send these multiple rows to another table called 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');


?> 

      

i get only the latest row data sent to the list table, then an error appears - invalid query, duplicate query.

please, help.

+3


source to share


1 answer


You must use single quotes to wrap strings in SQL



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

      

0


source







All Articles