我目前正在爲一個php用戶管理系統工作。我能夠創建新用戶。我可以更新我登錄的用戶帳戶 - 名字,姓氏,電子郵件和密碼。我創建了一個可以在表格中生成用戶信息的頁面。 它顯示用戶標識,用戶名,名字,姓氏,電子郵件地址以及刪除和編輯鏈接。刪除腳本起作用。編輯鏈接轉到表單,以便我可以更新用戶信息。我遇到的問題是:當我更新用戶的信息時,它不會在數據庫中更新它。 我只能更新用戶的信息,如果我以該用戶身份登錄。我正在使用會話,我認爲這與我的問題有關。這是代碼。我按下提交來更新用戶信息後運行此腳本。任何幫助,將不勝感激。謝謝。更新查詢不會更新,但運行時不顯示錯誤
<?php
require("connect.php");
if(empty($_SESSION['user'])) {
header("Location: ../hound/login.php");
die("Redirecting to ../hound/login.php");
}
$array_value = array(
':email' => $_POST['email'],
':first_name' => $_POST['first_name'],
':last_name' => $_POST['last_name'],
':id' => $_POST['id']
);
$query = "UPDATE users
SET
email = :email,
first_name = :first_name,
last_name = :last_name
WHERE
id = :id";
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($array_value);
}
catch(PDOException $ex) {
die("Failed to run query: " . $ex->getMessage());
}
header("Location: users.php");
die("Redirecting to users.php");
?>
這是編輯用戶信息的表單。一旦按下提交按鈕,它將運行上面的腳本。然後返回到一個名爲users的頁面。
<?php
require("../scripts/common.php");
if(empty($_SESSION['user']))
{
header("Location: ../hound/login.php");
die("Redirecting to ../hound/login.php");
}
$query_parm = array(
':id' => $_GET['id']
);
$query = "
SELECT
*
FROM users
WHERE
id = :id
";
try
{
$stmt = $db->prepare($query);
$stmt->execute($query_parm);
}
catch (PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$rows = $stmt->fetchAll();
?>
<form action="../scripts/edit_users.php" method="post">
<?php foreach($rows as $row): ?>
Username:<br />
<b><?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8'); ?></b>
<br /><br />
First Name:<br />
<input type="text" name="first_name" value="<?php echo htmlentities($row['first_name'], ENT_QUOTES, 'UTF-8'); ?>" />
<br /><br />
Last Name:<br />
<input type="text" name="last_name" value="<?php echo htmlentities($row['last_name'], ENT_QUOTES, 'UTF-8'); ?>" />
<br /><br />
E-Mail Address:<br />
<input type="text" name="email" value="<?php echo htmlentities($row['email'], ENT_QUOTES, 'UTF-8'); ?>" />
<br /><br />
Password:<br />
<input type="password" name="password" value="" /><br />
<br /><br />
<input type="submit" value="Update User" />
<a href="../scripts/users.php">Back</a><br />
<?php endforeach; ?>
'':id'=> $ _POST ['id']'from where這是從哪裏來的?我無法看到你的表格,或者我太困了? –
我認爲Hendra Nucleo是正確的。你的表單不提供'id'添加,因爲隱藏的輸入可能會使事情工作。目前您正在嘗試更新id = NULL的位置。在開發過程中,您可能需要添加print_r($ _ POST)以準確查看錶單提供的內容。 –