0

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.

6
  • 6
    Get rid of the single quotes around your placeholders. They are not needed. Commented Aug 30, 2015 at 21:35
  • Where do you send the $_GET['id']? Your form is sending a POST so maybe that whole block is never running? If you are sending that then see comment above. Commented Aug 30, 2015 at 21:37
  • @JohnConde your answer worked! I stole this of someone elses code. Now when I go to ...../post.php?id=19 is says: Notice: Undefined index: name in /config.php on line 72 Notice: Undefined index: age in /config.php on line 73 Am I missing something here ? Commented Aug 30, 2015 at 21:51
  • 1
    @BasSchreuder you'll still need to submit the form to send the name and age values via POST. Change your form's action attribute value to post.php?id=19 Commented Aug 30, 2015 at 23:12
  • @Phil that still does not work. Maybe a link will work: You have seen my code. Here is the link: schreudergroup.com/bas/post.php?id=26. Commented Aug 31, 2015 at 18:04

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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">

1 Comment

this does not work either. Is just sends me to age page post.php?id= after changing some syntax mistakes in your code.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.