mysql - php editing record, how to include current id -
i trying add functionality cooking book, can't think way of including id clicking edit.
the table called "chefs", , 3 columns; -id auto_increment - name - country
then, have list on index.php displaying chefs list:
<?php $chefs = get_chefs(); ?> <h3>chefs' list</h3> <ul> <?php foreach ($chefs $chef) { echo '<li>' . $chef['name'] . ' ' . $chef['country'] . '<a class="tolink" href="edit_chef.php?id=' . $chef['id'] . '" title="edit chef">edit chef</a>' . '</li>'; } ?> </ul>
on functions php, have get_chefs() function , find_chef_by_id($id) function:
function get_chefs() { include'db_connection.php'; try { return $conn->query("select * chefs"); } catch (pdoexception $e) { echo 'error:' . $e->getmessage() . "<br />"; return array(); } return true; } function find_chef_by_id($id = ':chef_id') { include 'db_connection.php'; $sql = 'select * chefs id=:chef_id'; try { $results = $conn->prepare($sql); $results->bindparam(':chef_id', $chef_id, pdo::param_int); $results->execute(); } catch(pdoexception $e) { echo 'error: ' . $e->getmessage() . '<br />'; return array(); } return $results->fetchall(pdo::fetch_assoc); }
to process edition of chef, created file called edit_chef.php:
if ($_server['request_method'] == 'post') { $name = filter_input(input_post, 'name', filter_sanitize_string); $country = filter_input(input_post, 'country', filter_sanitize_string); if(empty($name)) { $error_message = "chef's name can not empty"; } elseif (empty($country)) { $error_message = "country can not empty"; }else { if(edit_chef($name, $country)) { header('location: index.php'); exit; } else { $error_message = "could not update chef"; } } } include 'includes/header.php';?> <div class="col-container"> <h1>edit chef</h1> <?php if (isset($error_message)) { echo '<p class="message">' . $error_message . '</p>'; } $chefs = get_chefs(); foreach ($chefs $chef) { $id = find_chef_by_id($chef["id"]); ?> <form method="post" action="edit_chef.php?chef=<?php echo urldecode($id); ?>"> <div> <label for="name" class="required">name</label> <input name="name" type="text" value="<?php echo htmlentities($chef["name"]); ?>" /> </div> <div> <label for="country" class="required">country</label> <input name="country" type="text" value="<?php echo htmlentities($chef["country"]); ?>" /> </div> <button class="submit" type="submit" name="submit">submit</button> </form> </div>
but @ moment, don't know if because shouldn't loop foreach, or $id value form shouldn't find_chef_by_id($chef["id"]), when click on edit chef, goes right url "http://localhost/cooking_book/edit_chef.php?id=1" appears form each of chefs, not 1 clicked.
what doing wrong??
thank you
first mistake having $chef_id instead of $id in find_chef_by_id function, need fix that.
the other thing is, not understand why need loop around chefs in database if know id thats passed via address?
for example: in edit_chef.php need capture data:
<?php //get id url $chefid = null; if ( isset($_get['id']) ) $chefid = $_get['id']; if ( $chefid == null ) die ( "oops, chef id null, bad request?" ); //now find chef need $thechef = find_chef_by_id($chefid); //do stuff ?>
that should need fix.
also in form add hidden text field echo's out current id obtained via request:
<input type="hidden" name="chefid" value="<?=$chefid?>">
using method you're able pull chef id post data next time user presses submit button on edit form, not need custom action="blah" on form inside chef_edit.php
Comments
Post a Comment