[英]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.