-7

My issue is after submitting the form element in a php page, the page does not refresh. Here is my code.

<?php
    session_start();
    include('db.php');
    include('pages.php');
?>

form element

<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method = "POST">
   <table style="width:70%">
      <tr>
         <td width="10%">First Name</td>
         <td width="20%"><input style="border-radius: 4px; border: 2px solid gray; height: 20px; width: 180px;" type="text" name="fname" value="<?php echo $f_name;?>"></td>
      </tr>

<input style="border-radius: 5px; width: 100px; height: 25px;" type="submit" name="account_update" value="Update"/></td>
   </table>
</form>

if(isset($_POST['account_update']) && $_POST['account_update'] == 'Update'){

    $fname_edit = $_POST['fname'];

    $sql = $db->prepare("SELECT * from table where email = ?");
    $sql->bind_param("s",$email);
    $sql->execute();
    $sql = $sql->get_result();
    if(($getRowCount = $sql->num_rows) == 1){
    $updateQuery = $db->prepare("UPDATE info set first_name=? where email = ?");
                                       $updateQuery->bind_param("ss",$fname_edit,$email);
    $updateQuery->execute();
       }        
    }

 ?>

So clicking on update, inserts the data in the tale, but it only shows up on the page when I refresh it for a second time.

Also. pages.php is another page being fetched by including. My current page gets refreshed fine if I remove the include('pages.php'), but not with it. Any suggestions on how to get this thing out this would be of great help.

empiric
  • 7,825
  • 7
  • 37
  • 48

2 Answers2

1

You may redirect user to current file. After:

$updateQuery->execute();

Add:

header("Location: yourfile.php");
//header("Location: ".basename(__FILE__, '.php')); Might work too

This will redirect user to this file after executing query.

Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54
  • I don't think you get the question. They're already more than likely using that in their unshown code from another file they say they have it in. – Funk Forty Niner Jul 27 '16 at 16:54
  • @Fred-ii- Well, I don't see redirection in current file so I just added since I think that should solve the problem... Or I still don't understand the question. – Gynteniuxas Jul 27 '16 at 16:56
  • which is what I told the OP under their question. I fear a mix of probable errors/problems. I think they're outputting before header, but not checking for errors. – Funk Forty Niner Jul 27 '16 at 16:56
  • Ok, I see. I misunderstood it then. Should I delete my answer? – Gynteniuxas Jul 27 '16 at 16:57
  • that's up to you. But wait for the OP if you want. Yet I suspect 1 or 2 things wrong with their (unknown) code. – Funk Forty Niner Jul 27 '16 at 16:58
  • header('location: samepage.php') tried. it doesnt work – Bishwaroop Chakraborty Jul 27 '16 at 17:00
  • 1
    @BishwaroopChakraborty so are you only answering us from "answers" given? I've posted quite a few comments under your question but failed to respond. *"it doesnt work"* isn't much for anyone to go on. So, I will pass on this now, given no comments. Good luck, I'm out. – Funk Forty Niner Jul 27 '16 at 17:01
  • 1
    @BishwaroopChakraborty You should provide complete code of your current form file since it's not quite clear (apart from possible suggestions with header(), header already sent and refresh meta http-equiv). – Gynteniuxas Jul 27 '16 at 17:10
  • @Edvin: I understand, but both my pages contain a whole set of 1000 lines of code, which is impossible to provide here or even scoop out parts from it. – Bishwaroop Chakraborty Jul 27 '16 at 17:20
  • @BishwaroopChakraborty Well, in that case you should search for possible errors. This could be a lot of things so I suggest to use like `exit();` line to see when redirection stops working. I can't suggest anything else anymore. – Gynteniuxas Jul 27 '16 at 17:22
0
// REFRESH PAGE
echo "<meta http-equiv='refresh' content='0'>";
Shane
  • 133
  • 11
  • 1
    While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. – Michael Parker Jul 27 '16 at 19:57