Bob Marks
Bob Marks

Reputation: 139

List 1 row and not all rows in php

For some reason this is returning everything from the member_details table when all i want is to return a single row that equals to the selected item in the drop down list.

Here is the php for it: i thought it would return the row which equaled the value of the drop down but its just listing it all.

    <?php
        if (isset($_POST['members'])) {
        $ResultSet = getTableResults("member_details");
        echo "<h1> Member Details </h1>";
        echo "<table border='1' cellpadding='6'>";
        echo "<tr> <th>Id</th> <th>Name</th> <th>Job</th> <th>Wage</th> <th>Hobby</th> ";

        foreach ($ResultSet as $row) {
            echo "<tr>";
            echo "<td>" . $row ['member_id'] . "</td>";
            echo "<td>" . $row['first_name'] . " " . $row ['second_name'] . "</td>";
            echo "<td>" . $row['job'] . "</td>";
            echo "<td>" . $row['wage'] . "</td>";
            echo "<td>" . $row['hobby'] . "</td>";
            echo "</tr>";
        }

        echo "<table>";
    }
    ?>

If you want to see more code to make more sense of it please ask and il edit and update the question.

 function getTableResults() {
    $sql = "SELECT DISTINCT member_details.member_id, members.first_name,      members.second_name, member_details.wage, member_details.job, member_details.hobby
       FROM members
       INNER JOIN member_details
       ON members.member_id=member_details.member_id";
    $mysqlConnection = getSQLConnection();

    $ResultSet = $mysqlConnection->query($sql);
    return $ResultSet;
  }

Upvotes: 0

Views: 63

Answers (1)

T3 H40
T3 H40

Reputation: 2426

You do not have any Condition while requesting your information, getTableResults() fetches all Data that is stored to your table. You need to change that function and add a WHERE xy condition. Probably something like this:

 function getTableResults( $id ) {
    $sql = "SELECT DISTINCT member_details.member_id, members.first_name,members.second_name, member_details.wage, member_details.job, member_details.hobby
       FROM members
       INNER JOIN member_details
       ON members.member_id=member_details.member_id WHERE member_details.member_id = '$id'";
    $mysqlConnection = getSQLConnection();

    $ResultSet = $mysqlConnection->query($sql);
    return $ResultSet;
  }

Then Call the function with the requred member-id as parameter. Anyway, I'd recommend you to inform yourself about prepared statements to prevent SQL-Injection here!

Upvotes: 3

Related Questions