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.

maxdangelo
  • 3,063
  • 5
  • 21
  • 25
Bas Schreuder
  • 172
  • 1
  • 13
  • 6
    Get rid of the single quotes around your placeholders. They are not needed. – John Conde Aug 30 '15 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. – chris85 Aug 30 '15 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 ? – Bas Schreuder Aug 30 '15 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` – Phil Aug 30 '15 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. – Bas Schreuder Aug 31 '15 at 18:04
  • Sorry, I forgot that an empty `action` attribute makes it submit to the current page, including any existing query parameters. I suggest you attach a debugger and step through the codes execution or at least add some logging. – Phil Aug 31 '15 at 23:56

3 Answers3

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.

Will B.
  • 17,883
  • 4
  • 67
  • 69
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">
Abdallah Alsamman
  • 1,623
  • 14
  • 22
0

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

Bas Schreuder
  • 172
  • 1
  • 13