我正在使用以下代码来更新记录。不好的是它不是在更新记录,而是在添加新记录。
我究竟做错了什么?我希望它更新记录而不是现在创建一个。
我的修改网址如下所示:http ://randomsite.com/modify.php?id=1
修改.php代码:
<?php
require_once("connect.php");
$query = "SELECT * FROM cars WHERE id = :id";
$result = $odb->prepare($query);
$result->execute(array(':id' => $_REQUEST['id']) );
while ($row = $result->fetch(PDO::FETCH_ASSOC)) { ?>
<h1>Modify a car</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Brand: <input type="text" name="brand" value="<?php echo $row['brand']; ?>" /><br />
Model: <input type="text" name="model" value="<?php echo $row['model']; ?>" /><br />
Year: <input type="text" name="year" value="<?php echo $row['year']; ?>" /><br />
ID: <input type="text" name="id" value="<?php echo $row['id']; }?>" /><br />
<input type="submit" value="Modify" />
</form>
<?php
if(isset($_POST['submit'])) {
$id = $_POST['id'];
$brand = $_POST['brand'];
$model = $_POST['model'];
$year = $_POST['year'];
$q = "UPDATE cars WHERE id = $_GET[id] (id, brand, model, year) VALUES(:id, :brand, :model, :year)";
$query = $odb->prepare($q);
$results = $query->execute(array(
":id" => $id,
":brand" => $brand,
":model" => $model,
":year" => $year,
));
}
?>