简体   繁体   中英

How can i submit multiple forms which are displayed in while loop to insert rows into mysql?

Iam making a daily report system. its just a hobby project. question is that how can i submit form that is displayed in while loop to sql query. as the form is in while loop, i cannot determine that how many rows are going to be inserted. so i cannot add multiple insert statements. i am thinking of showinginsert into statement in while loop also. can i do it somehow? there is some of the code to understand: Attendance.php: `

<form action="create.php" method="POST">
<?php
$query="SELECT * FROM users where class "; // SQL query to fetch all table data
$view_users= mysqli_query($conn,$query);    // sending the query to the database

//  displaying all the data retrieved from the database using while loop
while($row= mysqli_fetch_assoc($view_users)){
$id = $row['id'];  
$marksnamaz = $row['marksnamaz'];
$class = $row['class'];
echo "<tr >";
echo " <th scope='row' >{$id}</th>";
echo " <td >{$marksnamaz}<input name='name' type='hidden' value='{$marksnamaz}'></td>";
echo "<td><select name='att'><option value='Hazir'>Hazir</option><option value='gherhazir'>Gher Hazir</option><option value='rukhsat'>Rukhsat</option></select></td>";
echo "<td><input type='text' name='reason'></td>";
echo "<td>{$class}</td>";
echo "</tr>";
}  
?>
<input type="submit" name="create">
</form>

` The Create.php

`

<?php 
  if(isset($_POST['create'])) 
    {
        $name = $_POST['name'];
        $att = $_POST['att'];
        $reason = $_POST['reason'];
        $date = date("y-m-d");
        // SQL query to insert user data into the users table
        $query= "INSERT INTO att(name, att, reason, date) VALUES('{$name}','{$att}','{$reason}','{$date}')";
        $add_user = mysqli_query($conn,$query);
    
        // displaying proper message for the user to see whether the query executed perfectly or not 
          if (!$add_user) {
              echo "something went wrong ". mysqli_error($conn);
          }

          else { 
              }         
    }
?>

` please answer me fast i will be very thankful to you.

I expect that i should show mysql insert into statement also in a while loop so it adds new statements automatically based on number of rows it is recieving

Create the following statement:

INSERT INTO tbl (a,b,c) VALUES
    (1,2,'abc'),
    (3,2,'xabc'),
    (11,12,'y'),
    (19,29,'zzz')

Notes:

  • The lack of trailing comma; I recommend collecting an array of rows, then inserting the commas with implode(',', arr)
  • The rows would be in your 'loop' of unknown length.
  • Be sure to escape any strings as you build the them -- mysqli_real_escape_string() .
  • If you have 100 rows to insert, it will run about 10 times as fast as one at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM