Php列出数据与数据表

时间:2018-03-08 09:22:21

标签: php mysql

我有一个数据表,我列出了数据库中的3列。(实际上有25个) 我为每一行添加了一个提交按钮,并希望在用户单击页面底部的那个按钮时查看所有数据。 这是我的代码:

<?php
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM jobs";
if($result = mysqli_query($db, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = 
mysqli_fetch_array($result)){
            echo "<td><button type='submit' value=".$row['id']".">Show Details</a></td>";
            echo "<td>'" . $row['type'] . "</td>";
            echo "<td>" . $row['status'] . "</td>";
            echo "<td>" . $row['location'] . "</td>";
            echo "<td>" . $row["result"] . "</td>";
        }
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "<p class='lead'><em>No data.</em></p>";
}
} else{
    echo "ERROR: Could not able to execute 
$sql. " . mysqli_error($link);
}

// Close connection

mysqli_close($db);

?>

我尝试使用Post方法,但没有用。

1 个答案:

答案 0 :(得分:0)

你可以使用jquery这样做。

    <?php
    require_once 'config.php';
    // Attempt select query execution

if(!mysqli_select_db($conn,$db))
    die("Cant select to database");

        $sql = "SELECT * FROM jobs";
        if($result = mysqli_query($db, $sql)){
        echo "<table id='example'>";
        if(mysqli_num_rows($result) > 0){
            while($row = mysqli_fetch_array($result)){

                echo "<tr>";
                echo "<td><button type='submit' value=". $row['id'] . ">Show Details</a></td>";
                echo "<td>" . $row['type'] . "</td>";
                echo "<td>" . $row['status'] . "</td>";
                echo "<td>" . $row['location'] . "</td>";
                echo "<td>" . $row["result"] . "</td>";
                echo "</tr>";

            }
            // Free result set
            mysqli_free_result($result);
        } else{
            echo "<p class='lead'><em>No data.</em></p>";
    }
    echo "</table>";
    } else{
        echo "ERROR: Could not able to execute 
    $sql. " . mysqli_error($link);
    }

    // Close connection

    mysqli_close($db);

    ?>
    <script>
            $('#example').on('click', 'tbody .edit_btn', function () {
                var $row = $(this).closest("tr"),       // Finds the closest row <tr> 
                    $tds = $row.find("td");             // Finds all children <td> elements

                $.each($tds, function() {               // Visits every single <td> element
                    console.log($(this).text());        // Prints out the text within the <td>
                });

            });
    </script>