繁体   English   中英

PHP编辑记录,如何包含当前ID

[英]php editing record, how to include current id

我正在尝试为烹饪书添加一些功能,但是我想不出一种方法来包含要单击的ID。

该表称为“主厨”,共有3列; -id作为auto_increment-名称-国家

然后,在index.php上有一个列表,其中显示了厨师列表:

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

在函数php上,我有一个get_chefs()函数和find_chef_by_id($ id)函数:

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);
}

为了处理厨师的版本,我创建了一个名为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>  

但是目前,我不知道是否是因为我不应该使用foreach循环,或者当我单击“编辑”时,表单的$ id值不应该是find_chef_by_id($ chef [“ id”])厨师,它会转到正确的网址“ http://localhost/cooking_book/edit_chef.php?id = 1 ”,但它会为每个厨师显示一种形式,而不仅仅是我单击的那个。

我究竟做错了什么??

谢谢

第一个错误是在find_chef_by_id函数中使用$ chef_id而不是$ id,您需要解决该问题。

另一件事是,如果您已经知道通过地址传递的ID,我不明白为什么您需要在数据库中的所有厨师之间循环?

例如:在您的edit_chef.php中,您需要捕获GET数据:

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

?>

那应该是您需要解决的全部。

同样在表单中添加一个隐藏的文本字段,以回显通过GET请求获得的当前ID:

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

使用这种方法,您可以在下次用户按下编辑表单上的Submit按钮时从POST数据中获取厨师ID,因此,chef_edit.php中的表单上不需要自定义Action =“ blah”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM