如何使用while循环在同一个表中插入新行

I'm trying to make 3 tables, I've used this while loop, the problem that I didn't know how to make the loop insert the new row in the same table. It makes new tables for each new row or just mess it up by putting information that should appear in another table.

Please help me.

What I got:

contract    name    cost    period
 210         sama     20        3


id         date    account    visit
2         3-jun      20        3
...
contract    name    cost    period
200         mama     10        3

id         date    account    visit
4          10        3         2

....
contract    name    cost    period
210         ana      50        6

id         date    account    visit
6          50        6         5

What I want:

contract    name    cost    period
210         sama     20        3
200         mama     10        3
210         ana      50        6


id         date    account    visit
2         3-jun      20        3
4          10        3         2
6          50        6         5

This is my code:

<?php while($row = mysqli_fetch_array($search_result)):?>

 <div style="font-size:1.5rem;  padding:0px; margin:0px; position:relative; width:95%; " dir="rtl">
      <h1>contract </h1>
 <table bgcolor="#fff" align="center" width="70%" border="3" dir="rtl" style="font-size:1.5rem; ">
 <tr>

  <td>name</td>
  <td>cost</td>
     <td>period</td>



  <tr>

  <td><?php echo $row['contract_num'];?></td>   
  <td><?php echo $row['cost'];?></td>
   <td><?php echo $row['bla'];?></td>
  <td><?php echo $row['serv'];?></td>


   </tr>
   </table></div>


 <div style="font-size:1.5rem;  padding:0px; margin:0px; position:relative; width:95%; " dir="rtl">
  <h1>visits</h1>
 <table bgcolor="#fff" align="center" width="70%" border="3" dir="rtl" style="font-size:1.5rem;  ">
 <tr>
  <td>id</td>    

     <td>date</td>    
  <td>account</td>  
     <td>visit</td>

     <tr>

  <td><?php echo $row['id'];?></td>

      <td><?php echo $row['date'];?></td>  
          <td><?php echo $row['account'];?></td>

      <td><?php echo $row['visit'];?></td>  
     </tr>
     </table></div>


     <?php endwhile;?>

Read more on tables: https://www.w3schools.com/html/html_tables.asp

thead - define table header. It has one row (tr) with your table's headers (th)

tbody - set the data for the columns. tr = table row, td = table data, or table cell

<table>
    <thead> <!-- define the header outside the while, it only needs to be defined once -->
        <tr>
            <th>Contract</th>
            <th>Name</th>
            <th>Cost</th>
            <th>Period</th>
        </tr>
    </thead>
    <tbody> <!-- in the body loop through all results and print one row for each result -->
        <?php while($row...): ?>
            <tr>
                <td><?php echo $row['contract'] ?></td>
                <td><?php echo $row['name'] ?></td>
                <td><?php echo $row['cost'] ?></td>
                <td><?php echo $row['period'] ?></td>
            </tr>
        <?php endwhile; ?>
    </tbody>
</table>