Search code examples
phpheaderrefresh

Refreshing page upon successful sql command


I am working on a script that is responsible for refreshing the page after a successful update. I am using the GET method in order to pass information. I have a button called "read" which when clicked will update my database and is responsible for refreshing the page. Currently I am using this function create my table:

function fetch_message($patient_ssn, $physician_ssn) {
    global $conn, $key, $iv;

    //Create query to fetch data
    $query = "SELECT message_ID, message_body, date_message, time_message, read_message FROM message WHERE patient_SSN='$patient_ssn' AND physician_SSN='$physician_ssn'";
    $result = mysqli_query($conn, $query);
    while($row = mysqli_fetch_row($result)) {
        //Enter table data here
        if($row[4] == 0) {
            echo '<tr class="table-danger">';
        } elseif($row[4] == 1) {
            echo '<tr class="table-success">';
        }
            echo "<td>$row[0]</td>";
            $decrypted_message = decrypt_text($row[1], $key, $iv);
            echo "<td>$decrypted_message</td>";
            echo "<td>$row[2]</td>";
            echo "<td>$row[3]</td>";
            //Checkbox setup
            if($row[4] == 0) {
                echo "<td><input type='submit' name='read' value='Confirm $row[0]'</td>";
            } elseif($row[4] == 1) {
                echo "<td><lable>Confirmed</lable></td>";
            }

        echo '</tr>';
    }
    echo '<tr>';
    echo '<th class="text-center" colspan="2" scope="row"><input type="submit" value="Update" name="update"</th>';
    echo '</tr>';
}

And this function to call my update:

function update_check($message_id, $patient_ssn, $physician_ssn) {
    global $conn;

    $query = "UPDATE message SET read_message='1' WHERE message_ID='$message_id' AND patient_SSN='$patient_ssn' AND physician_SSN='$physician_ssn'";
    $result = mysqli_query($conn, $query);
    if($result) {
        echo "success";
    }
}

The issue I have is once the button is clicked, I want it to update the table and refresh the page back to messaging.php. This is how I am trying to redirect

fetch_message($_SESSION['ssn'], $_SESSION['phy_ssn']);
                            //Update button has been clicked
                            if(isset($_GET['read'])) {
                                $message_id =explode(' ' , $_GET['read']);
                               update_check($message_id[1], $_SESSION['ssn'], $_SESSION['phy_ssn']);
 echo "<meta http-equiv='refresh' content='0' URL='messaging.php'>";
}

(I have also tried using header("Location: messaging.php"); but I get an error saying:

header cannot be modified

Probably, because I'm using session_start(); at the top of my script. With the meta tag it works, but it starts doing rapid refreshes without stopping.

The url looks like this:

http://localhost/455Project/messaging.php?read=Confirm+2 

I am quite puzzled as to what the proper process to implement this is. How do I solve this problem?


Solution

  • You can avoid having problem with headers by using output buffer. just add ob_start() after session_start().

    BTW. try to use prepared sql statements. It is safer.