How to generate master detail form in php mysql

I am looking forward to generate following report in php my sql from two database table . (here is my details )
I successfully store form value in database in two table (student and Subject). I can display record easily indivually. But when I have to display record in following image shape, then I dont knew how can I generate it ??? Can I get any help or example from this forum

123

How far have you got?

I can display student record but there are more than one subject against one student, There I am confused how to display these

It would be helpful for people helping you to see the actual code.

1 Like

                    <?php
                    // Include config file
                    require_once "connection.php";
                  
                  $sql = "SELECT * FROM student  WHERE  id = '$id";
                    if($result = mysqli_query($link, $sql)){
                        if(mysqli_num_rows($result) > 0){                                                             
                                       
					
                                while($row = mysqli_fetch_array($result)){
                                    echo "<tr>";
                                        
                                        echo "<td>" . $row['ID'] . "</td>";
					echo "<td>" . $row['Name'] . "</td>";
					echo "<td>" . $row['age'] . "</td>";
					echo "<td>" . $row['Class'] . "</td>";
					}}
							


    
                  $sql = "SELECT * FROM Subject  WHERE  Parentid = '$id";
                    if($result = mysqli_query($link, $sql)){
                        if(mysqli_num_rows($result) > 0){                                                             
                                       
					
                                while($row = mysqli_fetch_array($result)){
                                    echo "<tr>";
                                        
                                        echo "<td>" . $row['ParentID'] . "</td>";
					echo "<td>" . $row['Subject'] . "</td>";
					echo "<td>" . $row['Marks'] . "</td>";
				
					}}	
                                     
                                                                   mysqli_free_result($result);
                        } 
 
                    // Close connection
                    mysqli_close($link);
                    ?>

Apart from not opening or closing the top table, not opening the new table for the subjects (and closing it after the end of the loop), and not closing the table row for each individual subject, that seems to be about right.

You should be using a Prepared Statement instead of just sticking the id into your queries like that, but you can come back to that once it’s all working. What you have now generates invalid HTML.

1 Like

ok thanks

Instead of two querries, can ui used join ???
and if yes then When I use lastinsert id, then parents child report can be extract in same way as I display in image ?

You could use a single query to join the tables, but for the output that you want to produce - if it’s just a single form for a single person and their subjects - I don’t see that it will make things any easier.

Not sure what you mean about lastInsertID, sorry. If you want to display this information immediately after a query that stored it in the database, then yes, you could use the last-insert-id to retrieve it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.