简体   繁体   中英

Display records from MYSQL using PHP only if insert occurs

The PHP function given below displays the latest inserts one the page is loaded, but I want to refresh the data every time a row is inserted. I have heard of ajax, but I am not very familiar with it. Can anyone give me some guidance?

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="OPlayout.css"/>
    <title>Operator View</title>
</head>
<body>
    <div id="whole">
        <section id="mid_section"></br>
            <h2>Requests</h2></br>

            <?php include 'functions.php';
                currentRequests();
            ?>
        </section>
    </div>
</body>
</html>

PHP function :

function currentRequests(){
    //Include database connection file
    include 'databaseConnect.php';

    //Call global function to connect to db
    $connection = connectToDatabase();

    $queryToRun = "SELECT * FROM table_user_request
    WHERE 1=1
    AND date(entry_date) = date(now())
    ORDER BY entry_date DESC";

    $result = mysqli_query($connection, $queryToRun);

    //Create a table to store the results
    echo "<table border = '1' style='max-width:1180px;font-size: 14px;' >
        <tr>
            <td style='width:120px '>Name</td>
            <td style='width:120px '>Location</td>
            <td style='width: 20px '># of Riders</td>
            <td style='width:120px '>Destination</td>
            <td style='width:150px '>Email</td>
            <td style='width:100px '>Telephone</td>
            <td style='width:50px '>Date</td>
            <td style='width:50px '>Status</td>
            <td style='width: 60px '>Vehicle</td>
            <td style='width:50px '>Cancellation Reason</td>
            <td style='width: 60px '>Priority</td>
        </tr>";

    //Print records until done
    while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
        //echo("<table border = '1' style='width:1200px'>");
        //Output results found in the database
        echo("<tr>
            <td>" . $row['1']
                . "</td> <td>" . $row['3']
                . "</td> <td>" . $row['5']
                . "</td> <td>" . $row['4']
                . "</td> <td>" . $row['8']
                . "</td> <td>" . $row['9']
                . "</td> <td>" . $row['10']
                . "</td> <td>" . $row['11']
                . "</td> <td>" . $row['12']
                . "</td> <td>" . $row['13']
                . "</td> <td>" . $row['14'] . "</td>
            </tr>");
    }

    echo("</table>");
}

Quick tip: You don't need 1=1 in your SQL, it will fetch all possible rows on default. I'm fairly sure that if you have your column data types set up properly, you don't need to cast date() .

SELECT * FROM table_user_request
        WHERE date(entry_date) = date(now())
        ORDER BY entry_date DESC

I'm unsure of really what the question is, do you want a delay between rows being loaded? What do you mean by refresh the data? What Ajax would do for you is allow data to be dynamically loaded without reloading the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM