I have a webpage that displays a table from mysql and has a form which allows the user to add more rows to that table. After clicking the submit button, the new row is successfully added into the table by looking at mysql command in the console, but in the webpage (the same page as before), it's not. I have to reload the page, and then it displays the new row, but this causes that new row to be added into the table again by checking in the mysql console.
I partially solved this problem by placing a header in here:
while ( list ($key, $val) = each ($_POST) ) :
{
if($key == "submit_row")
{
$button=$val;
header("Location: myPosting.php");
}
}
endwhile;
As it detects the button is clicked, it signs the value to $button, below this code somewhere, I have a if-statement that if($button=="value of $val"), values in $_POST will be insert into mysql. If all code executes quickly, by the time INSERT statement is finished, header would then cause the page reloaded, and the page will display the added new row.
When I added a file upload in the form, it takes more time to execute the whole code. Then problem comes. header() executes first, it even has a empty $_POST replaced the previous one which contains the user's input, which results in no value is added into the table in mysql. If I remove that header(), it successfully inserts into the table, but has the problem I described in the beginning of this post. If I put header() after INSERT statement, it says 'Warning: Cannot modify header information - headers already sent by ... '
So where should I put that header()? Or
how should I reload the page in order to display the newly added row to the table if I want to do this within one page?