0

I have a table where one of the cell can be click and will open a popup modal to show more info about the data. Based on the data, jobregister_id, customer_name, machine_type, job_description, reason and jobregisterlastmodify_at will be fetch from job_register table and username (in the table picture it is assistant) will be fetch from assistants table. My problem is if that row do not have username it will not fetch the other data from the job_register table. Can someone help me to modify my code so even when that row did not have assistant, it can still show the rest data from job_register table.

table example

Table code with popup modal:

<!-- Incomplete Job -->
<?php
    include 'dbconnect.php';
    
    $numRow = "SELECT * FROM job_register LEFT JOIN assistants ON job_register.jobregister_id=assistants.jobregister_id 
               WHERE job_register.job_status = 'Incomplete' AND (job_register.job_cancel = '' OR job_register.job_cancel IS NULL)
               LIMIT 50";
    
    $numRow_run = mysqli_query ($conn,$numRow);
    
    if ($row_Total = mysqli_num_rows($numRow_run)) {
        echo '<b>Incomplete Job - '.$row_Total.'</b>';
    }
    
    else {
        echo '<b>Incomplete Job - No Data</b>';
    }
?>
    
    <table class="table table-bordered" id="auto" style="box-shadow:none; border-color: black; background-color:#ffffff;">
        <thead style="box-shadow:none;">
            <tr>
                <th style="border-color: black;"></th>
                <th style="border-color: black;">Leader</th>
                <th style="border-color: black;">Assistant</th>
                <th style="border-color: black;">Place</th>
                <th style="border-color: black;">Machine</th>
                <th style="border-color: black;">Reason</th>
                <th style="border-color: black;">Last Update Date</th>
            </tr>
        </thead>
        
        <?php
            
            include 'dbconnect.php';
            
            $results = $conn->query("SELECT * FROM job_register LEFT JOIN assistants ON job_register.jobregister_id=assistants.jobregister_id 
                                     WHERE job_register.job_status = 'Incomplete' AND (job_register.job_cancel = '' OR job_register.job_cancel IS NULL)
                                     ORDER BY job_register.job_assign ASC, job_register.jobregisterlastmodify_at DESC LIMIT 50");
                    
            while($row = $results->fetch_assoc()) {
                          
            $jobregisterlastmodify_at = $row['jobregisterlastmodify_at'];
            $datemodify = substr($jobregisterlastmodify_at,0,11); 
        ?>
        
        <tbody>
            <tr>
                <td style="border-color: black;"></td>
                <td style="border-color: black;" class="clickable-cell" data-row-id="<?php echo $row['jobregister_id']?>"><?php echo $row['job_assign']?></td>
                <td style="border-color: black;"><?php echo $row['username']?></td>
                <td style="border-color: black;"><?php echo $row['customer_name']?></td>
                <td style="border-color: black;"><?php echo $row['machine_type']?> - <?php echo $row['job_description']?></td>
                <td style="border-color: black;"><?php echo $row['reason']?></td>
                <td style="border-color: black;"><?php echo $datemodify ?></td>
            </tr>
        </tbody>   
        <?php } ?>
    </table>
    
    <!-- Job Info Popup Modal -->
    <div class="modal fade" id="popup-modal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Job Information</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                
                <div class="modal-body">

                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        $(document).ready(function(){
            $(".clickable-cell").click(function(){
                var jobregister_id = $(this).data("row-id");
                $.ajax({
                    url: "AdminJobTableJobInfo.php",
                    type: "post",
                    data: {jobregister_id: jobregister_id},
                    success: function(response){
                        $("#popup-modal .modal-body").html(response);
                        $("#popup-modal").modal("show");
                    }
                });
            });
        });
    </script>
<!-- End of Incomplete Job -->

Code to fetch data in popup modal:

<?php
    include 'dbconnect.php';
    
    $jobregister_id = $_POST["jobregister_id"];
    
    $query = "SELECT * FROM job_register LEFT JOIN assistants 
              ON job_register.jobregister_id=assistants.jobregister_id 
              WHERE job_register.jobregister_id = '$jobregister_id' AND assistants.jobregister_id = '$jobregister_id' ";

    $result = mysqli_query($conn, $query);
    
    // Check if query was successful
    if (!$result) {
        die("Query failed: " . mysqli_error($conn));
    }
    
    $html = '';
    while ($row = mysqli_fetch_assoc($result)) {
        // Create HTML for the popup modal
        $html .= "<h4>Row ID: " . $row["jobregister_id"] . "</h4>";
        $html .= "<p>Leader: " . $row["job_assign"] . "</p>";
        
        if ($row["username"] !== NULL) {
            echo $html;
        } else {
            // Create HTML for the popup modal when $html is empty
            $html = "<p>No data available for this job register ID.</p>";
            echo $html;
        }
                
        $html .= "<p>Place: " . $row["customer_name"] . "</p>";
        $html .= "<p>Machine: " . $row["machine_type"] . " - " . $row["job_description"] . "</p>";
        $html .= "<p>Reason: " . $row["reason"] . "</p>";
        $html .= "<p>Last Update Date: " . $row["jobregisterlastmodify_at"] . "</p>";
        $html .= "<hr>";
    }
    
    // Check if $html is not empty
    if (!empty($html)) {
        echo $html;
    } else {
        // Create HTML for the popup modal when $html is empty
        $html = "<p>No data available for this job register ID.</p>";
        echo $html;
    }
?>

Here is some other query that I've tried but non of it is working:


SELECT * FROM job_register INNER JOIN assistants ON job_register.jobregister_id = assistants.jobregister_id WHERE job_register.jobregister_id = '$jobregister_id'


SELECT * FROM job_register LEFT OUTER JOIN assistants ON job_register.jobregister_id = assistants.jobregister_id WHERE job_register.jobregister_id = '$jobregister_id

Anonymous
  • 9
  • 3
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Apr 05 '23 at 08:44
  • _In that case_ all variations of `[LEFT] [OUTER|INNER|] JOIN` work the same. In other words, that is not the problem. – Rick James May 01 '23 at 17:01
  • Please show a few rows of data and indicate which ones work or fail. – Rick James May 01 '23 at 17:01

0 Answers0