如何防止PHP代码在加载页面时运行

Good day,

Although I'm an old programmer, I'm new to php, so I need help.

Context 1. I have a submit form with an update button. 2. I have php code to insert records in a MySQL database.

Problem 1. When I first start the page, the php code runs. 2. When I refresh the page, the php code runs again.

Code

<!-- Button definition -->  
<input id="submit" type="submit" class="button" name="submit" value="submit"     onclick="Update_MySQL()" /> 

<!-- Function to be executed by the onclick --> 
<script>
<?php 
function Update_MySQL() {
if(isset($_POST['submit'])) 
{
$sql = "MYSQL UPDATE STRING" ;
$MY_CONNECTION->query($sql) ; 
}
}   
?>
</script>

What I know 1. PHP runs on server and javascript on the browser (client) 2. PHP delivers data to the server when the page is launched or refreshed

Can anyone help me on this?

Many Thanks

You can't prevent this when using the native html <form action="form.php"> way, but there is a way around. After progressing your form, you can redirect using header() to another page (or even the same) with a GET-Redirection-Code (see wikipedia).

if (isset($_POST['field'])) {
    // progress data
    header('HTTP/1.1 303 See Other');
    header('Location: '.$_SERVER['PHP_SELF']);
    exit;
}

Another completely different but also working approach is to use javascript to submit your form data.