update data via php in mysql DB -
i have looked on www , forum, not find answer work. want update post on .........../post.php?id=19
this 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' id=':id'"; $query = $conn->prepare($sql); $query->execute( array( ':name'=>$name, ':age'=>$age, ':id' => $id )); } }
and form:
<h3>update 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 post returns old record without update filled in form.
hope can assist me, million. bas
ps. $conn correct , works when insering or printing posts.
couple issues is, 1 view profile page $_get['id']
set, execute update_user function regardless if form submitted or not. should check value ensure form submitted. other issue sql 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 id = :id"; $query = $conn->prepare($sql); $query->execute(array(':name' => $name, ':age' => $age, ':id' => $id)); } } ?> <h3>update 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 hidden input field id , changed condition update_user
check post id instead of id.
and love of programming, please validate $_post data before sending database.
Comments
Post a Comment