Home » Php » How to display two data table in one php?? But it ony come out a table record

How to display two data table in one php?? But it ony come out a table record

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to put two table record into one PHP, but it only views one of the records when including both PHP file?
another file when echo will become an undefined index.
Main.php

<?php 
include"Location.php";
include"Fullname.php";
 ?>
<html>
<head>
<table id ="table">
<tr>
<th>Customer Name</th>
<th>Location From</th>
<th>Location To</th>
<th>Departure Date</th>
<th>Return Date</th>
<th>Travel</th>
</tr>
<?php
if(isset($result ) && !empty($result )){
    while($row = $result->fetch_assoc()){
        echo "
    <tr>
    <td>{$row['Fullname']}</td>}}
    <td>{$row['LocationFrom']}</td>
    <td>{$row['LocationTo']}</td>
    </tr>\n";
    }
}
 ?>
 </table>   
 </body>
 </html>

Location.php

<?php

$user=filter_input(INPUT_POST,'user');
$fn=filter_input(INPUT_POST,'fn');
$email=filter_input(INPUT_POST,'email');
$ps=filter_input(INPUT_POST,'ps');
$rps=filter_input(INPUT_POST,'rps');
$from=filter_input(INPUT_POST,'from');
$to=filter_input(INPUT_POST,'to');
$departure=filter_input(INPUT_POST,'departure');
$return=filter_input(INPUT_POST,'return');
$flight=filter_input(INPUT_POST,'flight');

$servername="localhost";
$dbusername="root";
$dbpassword="";     
$dbname="register";


$conn=new mysqli($servername,$dbusername,$dbpassword,$dbname);

if(mysqli_connect_errno()){

die('Connection Error('. mysqli_connect_errno() .')'. mysqli_connect_error());
}
else{

$sql = "SELECT * FROM booking";
$result = mysqli_query($conn,$sql);


mysqli_close($conn);
}
?>

Fullname.php

<?php

$user=filter_input(INPUT_POST,'user');
$fn=filter_input(INPUT_POST,'fn');
$email=filter_input(INPUT_POST,'email');
$ps=filter_input(INPUT_POST,'ps');
$rps=filter_input(INPUT_POST,'rps');
$from=filter_input(INPUT_POST,'from');
$to=filter_input(INPUT_POST,'to');
$departure=filter_input(INPUT_POST,'departure');
$return=filter_input(INPUT_POST,'return');
$flight=filter_input(INPUT_POST,'flight');

$servername="localhost";
$dbusername="root";
$dbpassword="";     
$dbname="register";


 $conn=new mysqli($servername,$dbusername,$dbpassword,$dbname);

 if(mysqli_connect_errno()){

die('Connection Error('. mysqli_connect_errno() .')'. mysqli_connect_error());
}
else{

$sql = "SELECT Fullname FROM useraccount";
 $result = mysqli_query($conn,$sql);


mysqli_close($conn);
 }
?>

Why it comes out the data only one table?? When have include “FullName.php” and included “Location.php”, it only displays fullname.php data only.
Picture with only one record

How to&Answers:

It is only showing one table because you are overridding the value.
for example, This is what you are doing
file1.php

a = 3

file2.php

a = 4

So when you include both files. file2.php will override the value of file1.php
so what you need to do is set file2.php‘s a variable to a different name. So …

b = 4

And now you will have both values 3 and 4 just in different variables.
In your case just do this

Main.php

<?php 
result = []
include"Location.php";
include"Fullname.php";
 ?>
<html>
<head>
<table id ="table">
<tr>
<th>Customer Name</th>
<th>Location From</th>
<th>Location To</th>
<th>Departure Date</th>
<th>Return Date</th>
<th>Travel</th>
</tr>
<?php
if(isset($result ) && !empty($result )){
    while($location_row = $result[0]->fetch_assoc() and $fullname_row = $result[1]->fetch_assoc()){
        echo "
    <tr>
    <td>{$fullname_row['Fullname']}</td>}}
    <td>{$location_row['LocationFrom']}</td>
    <td>{$location_row['LocationTo']}</td>
    </tr>\n";
    }
}
 ?>
 </table>   
 </body>
 </html>

Location.php

<?php

$user=filter_input(INPUT_POST,'user');
$fn=filter_input(INPUT_POST,'fn');
$email=filter_input(INPUT_POST,'email');
$ps=filter_input(INPUT_POST,'ps');
$rps=filter_input(INPUT_POST,'rps');
$from=filter_input(INPUT_POST,'from');
$to=filter_input(INPUT_POST,'to');
$departure=filter_input(INPUT_POST,'departure');
$return=filter_input(INPUT_POST,'return');
$flight=filter_input(INPUT_POST,'flight');

$servername="localhost";
$dbusername="root";
$dbpassword="";     
$dbname="register";


$conn=new mysqli($servername,$dbusername,$dbpassword,$dbname);

if(mysqli_connect_errno()){

die('Connection Error('. mysqli_connect_errno() .')'. mysqli_connect_error());
}
else{

$sql = "SELECT * FROM booking";
array_push($result, mysqli_query($conn,$sql));



mysqli_close($conn);
}
?>

Fullname.php

<?php

$user=filter_input(INPUT_POST,'user');
$fn=filter_input(INPUT_POST,'fn');
$email=filter_input(INPUT_POST,'email');
$ps=filter_input(INPUT_POST,'ps');
$rps=filter_input(INPUT_POST,'rps');
$from=filter_input(INPUT_POST,'from');
$to=filter_input(INPUT_POST,'to');
$departure=filter_input(INPUT_POST,'departure');
$return=filter_input(INPUT_POST,'return');
$flight=filter_input(INPUT_POST,'flight');

$servername="localhost";
$dbusername="root";
$dbpassword="";     
$dbname="register";


 $conn=new mysqli($servername,$dbusername,$dbpassword,$dbname);

 if(mysqli_connect_errno()){

die('Connection Error('. mysqli_connect_errno() .')'. mysqli_connect_error());
}
else{

$sql = "SELECT Fullname FROM useraccount";
 array_push($result, mysqli_query($conn,$sql));


mysqli_close($conn);
 }
?>

Basically I fixed the second problem by looping through the rows of both location and full name at the same time. So every row in full name database is associated with the row in location that has the same index. It is a bad way to structure you database, but I assume you are new so it works for your needs