这是我用来尝试更新记录的代码。我没有看到任何问题,但它不会引发错误并表示它已成功运行。我已经尝试了其他几种相同的方法,它只是不更新 MSSQL 表中的行。数据库/表在我运行 Windows 10 和 IIS 的机器上是本地的(我知道!!)。
<?php
/**
* Use an HTML form to edit an entry in the
* users table.
*
*/
require "C:\inetpub\wwwroot\Remediation\config.php";
require "C:\inetpub\wwwroot\Remediation\common.php";
if (isset($_POST['submit'])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password);
$user =[
"id" => $_POST['id'],
"firstname" => $_POST['firstname'],
"lastname" => $_POST['lastname'],
"email" => $_POST['email'],
"age" => $_POST['age'],
"location" => $_POST['location'],
"date" => $_POST['date']
];
$sql = "UPDATE users
SET id = :id,
firstname = :firstname,
lastname = :lastname,
email = :email,
age = :age,
location = :location,
date = :date
WHERE id = :id";
$statement = $connection->prepare($sql);
$statement->execute($user);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
if (isset($_GET['id'])) {
try {
$connection = new PDO($dsn, $username, $password);
$id = $_GET['id'];
$sql = "SELECT * FROM users WHERE id = :id";
$statement = $connection->prepare($sql);
$statement->bindValue(':id', $id);
$statement->execute();
$user = $statement->fetch(PDO::FETCH_ASSOC);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else {
echo "Something went wrong!";
exit;
}
?>
<?php if (isset($_POST['submit']) && $statement) : ?>
<blockquote><?php echo escape($_POST['firstname']); ?> successfully
updated.</blockquote>
<?php endif; ?>
<h2>Edit a user</h2>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo
escape($_SESSION['csrf']); ?>">
<?php foreach ($user as $key => $value) : ?>
<label for="<?php echo $key; ?>"><?php echo ucfirst($key); ?></label>
<input type="text" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo escape($value); ?>" <?php echo ($key === 'id' ? 'readonly' : null); ?>>
<?php endforeach; ?>
<input type="submit" name="submit" value="Submit">
</form>
我到底哪里错了。我可以很好地连接、删除和创建。我需要调整 $USER 或查询吗?