eve_mf
eve_mf

Reputation: 825

php editing record, how to include current id

I am trying to add some functionality to my cooking book, but I can't think about a way of including the id I am clicking to edit.

The table is called "chefs", and the is 3 columns; -id as auto_increment - name - country

Then, I have a list on index.php where I am displaying the chefs list:

<?php 
  $chefs = get_chefs();
?>

<h3>Chefs' list</h3>
  <ul>
    <?php foreach ($chefs as $chef) {
      echo '<li>' . $chef['name'] . ' from ' . $chef['country'] . '<a class="toLink" href="edit_chef.php?id=' . $chef['id'] . '" title="edit chef">Edit chef</a>' . '</li>';  
    } ?>
</ul>

On functions php, I have a get_chefs() function and a find_chef_by_id($id) function:

function get_chefs() { 
   include'db_connection.php';
    try {
        return $conn->query("SELECT * FROM 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 * FROM chefs WHERE 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 the edition of a chef, I created a 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 be empty";
    } elseif (empty($country)) {
        $error_message = "Country can not be 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 as $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 at the moment, I don't know if it is because I shouldn't loop with foreach, or my $id value for the form shouldn't be find_chef_by_id($chef["id"]), when I click on Edit chef, it goes to the right url "http://localhost/cooking_book/edit_chef.php?id=1" but it appears a form for each of the chefs, not just the one I clicked.

What am I doing wrong??

Thank you

Upvotes: 1

Views: 107

Answers (1)

Mark Phillips
Mark Phillips

Reputation: 273

First mistake is having $chef_id instead of $id in your find_chef_by_id function, you need to fix that.

The other thing is, i do not understand why you need to loop around all chefs in your database if you already know the ID thats passed via the address?

For example: In your edit_chef.php you need to capture the GET data:

<?php

    //Get the ID from the URL
    $chefID = null;
    if ( isset($_GET['id']) )
        $chefID = $_GET['id'];

    if ( $chefID == null )
        die ( "oops, Chef ID is null, bad request?" );

    //Now find the chef we need
    $theChef = find_chef_by_id($chefId);   

    //do stuff

?>

That should be all you need to fix.

Also in your form add a hidden text field that echo's out the current ID that you obtained via the GET request:

<input type="hidden" name="chefId" value="<?=$chefId?>">

Using this method you're able to pull the chef ID from the POST data next time the user presses the Submit button on the edit form, so you do not need a custom Action="blah" on the form inside chef_edit.php

Upvotes: 2

Related Questions