在mysql DB中通过php更新数据

I have looked all over the WWW and the forum, but could not find an answer to work. I want to update my post on .........../post.php?id=19

this is my function:

function update_user($conn) {

    if(isset($_GET['id'])) {
        $id = $_GET['id'];
        $name = $_POST['name'];
        $age = $_POST['age'];
        $sql = "UPDATE users SET name =':name', age = ':age' WHERE id=':id'";
        $query = $conn->prepare($sql);
        $query->execute( array( ':name'=>$name, ':age'=>$age, ':id' => $id ));
        }
 }

And this is my form:

 <h3>Update a user</h3>
 <?php update_user($conn) ?>
 <form name="myForm2" method="POST" action= "">
 <label>Name:</label><input type="text" id="name" name="name">
 <label>Age:</label><input type="text" id= "age" name="age">

 <input type="submit" value="add" name="update">

I have no errors but the POST just returns the old record without the update I filled in the form.

Hope someone can assist me, thanks a million. Bas

PS. the $conn is correct and works when insering or printing posts.

Couple issues is, 1 to view the profile page $_GET['id'] is set, so it will execute the update_user function regardless if the form is submitted or not. You should check for another value to ensure the form is submitted. The other issue is the SQL with named parameters should not use quotes.

<?php
function update_user($conn)
{
    if(isset($_POST['id'])) {
        $id = $_POST['id'];
        $name = $_POST['name'];
        $age = $_POST['age'];
        $sql = "UPDATE users SET name = :name, age = :age WHERE id = :id";
        $query = $conn->prepare($sql);
        $query->execute(array(':name' => $name, ':age' => $age, ':id' => $id));
    }
}
?>

 <h3>Update a user</h3>
 <?php update_user($conn) ?>
 <form name="myForm2" method="POST" action= "">
 <label>Name:</label><input type="text" id="name" name="name">
 <label>Age:</label><input type="text" id= "age" name="age">
 <input type="hidden" name="id" value="<?php echo $_GET['id'] ?>">
 <input type="submit" value="add" name="update">
 </form>

I added a hidden input field for id and changed the condition for update_user to check for the POST id instead of the GET id.

And for the love of all that is programming, please validate your $_POST data before sending it to a database.

read Phil's comment, that should do it, also you should add a $postId variable defining the current post id in it so the form sends the correct post to edit.

<form name="myForm2" method="POST" action= "post?id=<?= $postId ?>">
 <label>Name:</label><input type="text" id="name" name="name">
 <label>Age:</label><input type="text" id= "age" name="age">

 <input type="submit" value="add" name="update">

Well. I'll just create a update_post.php file and work that way. Thank for all the effort.