3

我正在使用以下代码来更新记录。不好的是它不是在更新记录,而是在添加新记录。

我究竟做错了什么?我希望它更新记录而不是现在创建一个。

我的修改网址如下所示: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,
    )); 
}
?>
4

2 回答 2

4

您的 SQL 应该看起来更像这样:

   $q = "UPDATE cars SET 
          id = :id, 
          brand = :brand, 
          model = :model, 
          year = :year 
          WHERE id = :oldid";

    $query = $odb->prepare($q);
    $results = $query->execute(array(
            ":id"    => $id,
            ":brand" => $brand,
            ":model" => $model,
            ":year"  => $year,
            ":oldid" => $_GET['id'],
    ));

附带说明一下,尽量不要在 SQL 中放入变量(例如 $_GET['id']),使用 PDO 的部分想法是避免这样做。

于 2012-06-26T23:10:31.473 回答
1

让它工作!

工作代码:

<?php
    require_once("connect.php");

    $query = "SELECT * FROM cars WHERE id = :id";

    $result = $dbh->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" name="submit"/>
    </form>

    <?php

    if(isset($_POST['submit'])) {
    $brand = $_POST['brand'];
    $model = $_POST['model'];
    $year  = $_POST['year'];
    $id    = $_POST['id'];

    $queryupdate = "UPDATE cars SET 
          brand= :brand, model= :model, year= :year WHERE id= :id";
    $q = $dbh->prepare($queryupdate);
    $q->execute(array(
            ":id"    => $id,
            ":brand" => $brand,
            ":model" => $model,
            ":year"  => $year));

            //Send them back to the page they were at/
header("location:admin.php");
    }

?> 
于 2012-06-27T13:39:27.540 回答