php 编辑记录,如何包含当前id
php editing record, how to include current id
我正在尝试向我的烹饪书添加一些功能,但我想不出一种方法来包含我正在单击以编辑的 ID。
table称为"chefs",是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"]),当我点击“编辑厨师”时,它会向右移动 url“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?>">
使用此方法,您可以在用户下次按下编辑表单上的提交按钮时从 POST 数据中提取厨师 ID,因此您不需要自定义 Action="blah" 里面的表格 chef_edit.php
我正在尝试向我的烹饪书添加一些功能,但我想不出一种方法来包含我正在单击以编辑的 ID。
table称为"chefs",是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"]),当我点击“编辑厨师”时,它会向右移动 url“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?>">
使用此方法,您可以在用户下次按下编辑表单上的提交按钮时从 POST 数据中提取厨师 ID,因此您不需要自定义 Action="blah" 里面的表格 chef_edit.php