ian305
ian305

Reputation: 25

Join one complete table and one column from another table inside the same database and display with PHP

I have two php files: index.php displays the test_data table with checkboxes. search.php will query the selected checked boxes from index.php table and also add a column from another pdf_report table called directory. My code is the following:

<?php
    $conn = mysqli_connect($servername, $username, $password, $database);
    if(isset($_POST['submit'])){
      if(!empty($_POST['checkbox_id'])){
        $data = implode("','",$_POST['checkbox_id']);
        $stmt = $conn-> prepare("SELECT  test_data.*,
                                         pdf_report.directory
                                FROM test_data
                                INNER JOIN pdf_report ON test_data.test_id=pdf_report.test_id WHERE test_data.test_id IN ('$data')");
        $stmt->execute();
        $result = $stmt-> get_result();
        while($row = $result->fetch_assoc()){
          echo "<tr>
                  <td> ".$row["test_id"]." </td>
                  <td> ".$row["can_udp_data_path"]." </td>
                  <td> ".$row["video1_path"]." </td>
                  <td> ".$row["video2_path"]." </td>
                  <td> ".$row["video3_path"]." </td>
                  <td> ".$row["video4_path"]." </td>

                  <td><div>
                        <button class='edit' id='" . $row['test_id'] . "'  >Run</button>
                      </div></td>

                  <td> ".$row["directory"]." </td>



                </tr>";
        }
      }else {
        echo '<span style="color:#ff0000;text-align:center;">No Test ID Selected!</span>';
      }
    }


    // Close connection
    mysqli_close($conn);
  ?>

I get the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM test_data INNER JOIN pdf_report ON test_da' at line 3.

Upvotes: 0

Views: 31

Answers (1)

user2579958
user2579958

Reputation: 26

You need to remove the comma after "pdf_report.directory," before your FROM.

Upvotes: 1

Related Questions