Whenever i execute this, only the last row of the loop gets inserted even if i click on the first row or any row.

What i need is: if i click register, the entire selected row must store in another table

Whats wrong here?
Please help me..

<table class="table table-striped jambo_table table-bordered"> <thead> <tr> <th width="75px">Semester:</th> <th width="110px">Subject Code:</th> <th>Subject Name:</th> <th width="110px">Exam Type:</th> <th  width="110px">Marks:</th> <th width="75px">Action:</th> </tr> </thead> <?php
    $view_users_query="select * from details WHERE regd_no='" .$_SESSION['regd_no']. "'";//select query for viewing users.
    $run=mysqli_query($dbcon,$view_users_query);//here run the sql query.

    while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
    {
        $slno=$row[0];
        $sem=$row[5];
        $subject_code=$row[9];
        $subject_name=$row[10];

    ?> <tr> 
    <td><input type="text" class="form-control" readonly="yes" name="sem1" id="sem1" value="<?php echo $sem;?>"></td> 
    <td><input type="text" class="form-control" readonly="yes" name="subject_code1" id="subject_code1" value="<?php echo $subject_code;  ?>"></td> 
    <td ><input type="text" class="form-control" readonly="yes" name="subject_name1" id="subject_name1" value="<?php echo $subject_name;  ?>"></td> 

    <td> <select class="form-control" id="etype" name="etype"> <option value="">Select-Exam-Type</option> 
    <option value="Backlog">Backlog</option> 
    <option value="Improvement">Improvement</option> 
    <option value="Special Exam">Special Exam</option> 
    </select> </td> 
    <td><select class="form-control" id="marks" name="marks"> <option value="">Marks</option> 
    <option value="100">100</option> 
    <option value="60">60</option> 
    <option value="30">30</option> </select></td>
    <td> <input type="submit" name="register/<?php echo $subject_code?>" class="btn btn-success form-control" value="Register" > </td> 
    <!--btn btn-danger is a bootstrap button to show danger--> </tr> 
    <?php
}
    ?><br> 
    <?php                   
if(isset($_POST['register']))
{

$regn=$_POST['regn'];//here getting result from the post array after submitting the form.
$name=$_POST['name'];//same
$branch=$_POST['branch'];
$batch=$_POST['batch'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$center=$_POST['center'];
$date=$_POST['date'];
$sem1=$_POST['sem1'];
$subject_code1=$_POST['subject_code1'];
$subject_name1=$_POST['subject_name1'];
$etype=$_POST['etype'];
$marks=$_POST['marks'];

 if(mysqli_num_rows($run_query)>0)
{
echo "<script>alert('Subject Name $subject_name1 with Subject Code $subject_code1 is already registered')</script>";
exit();
}
}

$insert_user="insert into registered (regd_no,name,branch,batch,email,mobile,center,sem,subject_code,subject_name,date,etype,marks) VALUE ('$regn','$name','$branch','$batch','$email','$mobile','$center','$sem1','$subject_code1','$subject_name1','$date','$etype','$marks')";

   if(mysqli_query($dbcon,$insert_user))
{
echo "<script>alert('You have Succesfully registered $subject_code1 - $subject_name1 ')</script>";
    echo"<script>window.open('','_self')</script>";
}

}

?> </table>

Dani AI

Generated

As pointed out, the root cause is that the page is not giving the server a unique, identifiable set of inputs for the row you clicked. When you render many inputs with the same names (or you overwrite variables in the loop and only process after the loop), the server will only see one final value for each name. Resolve that by making each row submit a clear identifier so the server knows which row to insert.

Two simple, reliable patterns:

<!-- Option A — one small form per row -->
<form method="post" action="">
  <input type="hidden" name="row_id" value="<?= $rowId ?>">
  <input readonly name="code" value="<?= htmlspecialchars($code) ?>">
  <select name="exam_type">...</select>
  <button type="submit" name="register_row" value="<?= $rowId ?>">Register</button>
</form>

<!-- Option B — single table form, submit button carries the row id -->
<form method="post" action="">
  <tr>
    <td><input readonly name="code[<?= $rowId ?>]" value="<?= htmlspecialchars($code) ?>"></td>
    <td><button type="submit" name="register_row" value="<?= $rowId ?>">Register</button></td>
  </tr>
</form>

Server-side handling (concept): check which button was clicked (the value carries the row id), then either re-query the canonical row from the DB by that id or read the posted field values for that id. Do not trust readonly inputs for security — re-validate on the server. Use prepared statements (PDO/mysqli) and add a UNIQUE constraint on the registration columns you want to prevent duplicate inserts.

Quick troubleshooting checklist:

  • No nested <form> tags; each submit must belong to the intended form.
  • Keep submit names simple (avoid slashes/special chars); put the row id in the button value.
  • Inspect the network/POST payload in browser devtools to confirm what is sent.
  • Enforce server-side validation and a DB unique index so accidental duplicate inserts are prevented.

These changes will let a click on any row submit exactly that row’s data instead of always inserting the last-rendered row.

If you're referring to this bit:

while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
    {
        $slno=$row[0];
        $sem=$row[5];
        $subject_code=$row[9];
        $subject_name=$row[10];
    ?>

you're setting the variables $slno, $sem, $subject_code and $subject_name on each loop through but doing nothing with them. On the next loop you're setting them again and, again, doing nothing with the new values.
Only on the last loop do you move on to the next piece of code and actually output the values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.