Denzil Bennett
Denzil Bennett

Reputation: 71

How do I get the HTML page to refresh correctly when I hit the submit button to send data to my SQL database?

Currently, I'm working on a small project for class using PHP, MySQL, and HTML to create a simple data entry web page where you enter in a Part Number, a Customer Number, the Quantity of parts, and the Price into an HTML form and once you hit Submit it sends the data to the MySQL database while (theoretically) updating the top part of the screen which shows the contents of the MySQL database.

The odd thing is whenever I hit the Submit button for the first time after making a new entry it doesn't seem to do anything. When you add another record and hit Submit the screen refreshes and you see the previous record entered displayed on the top half of the screen, even though there are two records in the database. As you add records, the records displayed are always one record behind what is actually in the database.

This lead me to believe it was some sort of refresh screen issue so I tried numerous approaches to screen refreshing with no results.

So, my question is this: is there a way for me to hit the submit button after making an entry and then having the screen refresh so that it shows the newly added record?

Here is my code for the project thus far:

<html>
<head>
<title>Homework Database - Sales Table </title>
</head>

<body>
    <h1>Homework Database - Sales Table</h1>
    <?php
    $mysqli = new mysqli('localhost', 'root', 'moebius1', 'homework'); // hostname , user, password, database name
    if ($mysqli->connect_error) 
    {
        die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
    }
    ?>
    <form action="sales2.php" method="POST">
    <input type="hidden" name="action" value="delete" />
    <table>
        <tr>
            <th width="10%" align="left"> Delete &nbsp&nbsp </th>
            <th width="10%" align="left"> ID Number &nbsp&nbsp </th>
            <th width="10%" align="left"> Part Number &nbsp&nbsp </th>
            <th width="10%" align="left"> Customer Number &nbsp&nbsp </th>
            <th width="10%" align="left"> Quantity &nbsp&nbsp</th>
            <th width="10%" align="left"> Price &nbsp&nbsp </th>
        </tr>
    <?php
        $result = $mysqli->query("SELECT * FROM sales");
        while($row = $result->fetch_assoc())
        {
            print "<tr>";
            print "<td><input type='checkbox' name='checkboxes[]' value='".$row["IDnumber"]."' /></td>";
            print "<td>".$row["IDnumber"]."</td>";
            print "<td>".$row["partNbr"]."</td>";
            print "<td>".$row["custNumber"]."</td>";
            print "<td>".$row["quantity"]."</td>";
            print "<td>".$row["price"]."</td>";
            print "</tr>";
        }
    ?>
    </table>
    <input type="submit" value="Delete"/>
    </form>
    <hr>
    <form action="sales2.php" method="POST" >
        <input type="hidden" name="action" value="insert" />
        Part Number: <input name="partNbr" /><br/>
        Customer Number: <input name="custNumber" /><br/>
        Quantity: <input name="quantity" /><br/>
        Price: $ <input name="price" /><br/>
        <input type="submit" value=" Submit " />
    </form> 
    <?php
        if(isset($_REQUEST["action"]))
        {
            switch($_REQUEST["action"])
            {
                case "insert":
                    $SQL="INSERT INTO sales (partNbr, custNumber, quantity, price)VALUES (";
                        $SQL=$SQL."'".$_REQUEST["partNbr"]."',";
                        $SQL=$SQL."'".$_REQUEST["custNumber"]."',";
                        $SQL=$SQL."'".$_REQUEST["quantity"]."',";
                        $SQL=$SQL."'".$_REQUEST["price"]."'";
                        $SQL=$SQL.");";
    if ($mysqli->query($SQL)=== FALSE)
    {
        printf("Error . Unable to insert data to table. ".$mysqli->error);
    }

                        break;
                case "delete":
                    $SQL="DELETE FROM sales WHERE";
                    for($i=0; $i<count($_REQUEST['checkboxes']); $i++)
                    {
                            $SQL=$SQL." IDnumber=".$_REQUEST['checkboxes'][$i]." or";
                    }
                    $SQL=rtrim($SQL, "or");
                    if ($mysqli->query($SQL)== FALSE)
                    {
                        printf("Error. Unable to delete value.".$mysqli->error);
                    }
                    break;
    }
    }
    $mysqli->close();
    ?>
</body>
</html>

Thanks in advance for any and all help.

Denzil Bennett

Upvotes: 0

Views: 726

Answers (2)

Burrito
Burrito

Reputation: 515

You are retrieving the data from your database before performing the insert.

The flow is this: page loads data from database and displays it. Then it checks if there is a request to modify the data - performing this action if necessary last.

If you move the last set of php tags to below the database connection section it should work fine. The flow is then: Checks for additions/removals then retrieves data and displays it.

Upvotes: 3

Sougata Bose
Sougata Bose

Reputation: 31739

Just reload the page after the operation.

header('location:yourpage.php');
exit;

Hope you problem will be solved.

Upvotes: 0

Related Questions