在表单中编辑行详细信息 - PHP MySQL

时间:2015-11-30 21:07:49

标签: php html mysql sql sql-update

这是我第一次尝试编辑数据库中的数据。我浏览并编辑任何值并提交,没有错误,但是当我查看表格时数据没有更新。这是显示我的“汽车”表中汽车细节的页面。

  // Retrieve Car Details
  $sql = "SELECT CarID, Make, Model, Year, Engine FROM Cars";
  $result = mysql_query($sql, $connection);

  //create table
  echo "<table>";

  // Loop through the data and then display chosen data
  echo "<h2>Car Details</h2>";
  echo "<tr><th>Car ID</th><th>Make</th><th>Model</th><th>Year</th><th>Engine</th><th>Delete</th><th>Edit</th></tr>"; 
  while($a_row = mysql_fetch_assoc($result)){
    //<a href="edit_car.php?CarID=' . $a_row['CarID'] . '"> passes the ID when the link is clicked
    echo "<tr><td>" . $a_row['CarID'] . "</td><td>" . $a_row['Make'] . "</td><td>" . $a_row['Model'] . "</td><td>" . $a_row['Year'] . "</td><td>" . $a_row['Engine'] . "</td><td><a href='?DeleteCar=" . $a_row['CarID'] . "' onclick=\"return confirm('Delete Car?')\">Delete</a></td><td>" . '<a href="edit_car.php?CarID=' . $a_row['CarID'] . '">Edit</a>' . "</td></tr>"; 
  }

  //close table                                     
  echo "</table>";

  // Close connection to Database
  mysql_close($connection);
  ?>

以下是处理更新的页面:

<body>
  <div class="add">
  <h2>Enter new details below</h2>
  <form action="" method="post">
    Make: <input type="text" name="Make"><br>
    Model: <input type="text" name="Model"><br>
    Year: <input type="text" name="Year"><br>
    Engine: <input type="text" name="Engine"><br>
    <input type="submit" value="submit">
  </form>

  <?php
  // Connecting to Database Server Code Not Displaying Details For Example

  // If connection cannot be made to Database Server
  if (!$connection)
    die("Cannot connect to Database");

  // Select the database
  mysql_select_db("db1520621", $connection)
    // If Database cannot be found
    or die("Cannot find Database");

  // Check form has been submitted
    $CarID = $_GET['CarID'];

    $Make = $_POST['Make'];
    $Model = $_POST['Model'];
    $Year = $_POST['Year'];
    $Engine = $_POST['Engine'];

    // Save data to database
    mysql_query("UPDATE Cars SET Make='$Make', Model='$Model', Year='$Year', Engine='$Engine' WHERE CarID=$CarID'")
    or die(mysql_error());

    echo("$Make $Model was updated");

  // Close connection to Database
  mysql_close($connection);
  ?>
  </div>
</body>

我收到错误消息:

注意:未定义的索引:在第46行的/home/stud/1/1520621/public_html/Car_Club_Task2/edit_car.php中进行注意:未定义的索引:/ home / stud / 1/1520621 / public_html / Car_Club_Task2 / edit_car中的模型第47行的.php注意:未定义的索引:第48行的/home/stud/1/1520621/public_html/Car_Club_Task2/edit_car.php中的年份注意:未定义的索引:/ home / stud / 1/1520621 / public_html / Car_Club_Task2中的引擎第49行的/edit_car.php注意:未定义的变量:第53行的/home/stud/1/1520621/public_html/Car_Club_Task2/edit_car.php中的CarID您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在''汽车'附近使用正确的语法SET'Make'='','Model'='','Year'='','Engine'=''WHERE 'CarID'='在第1行

可能错过了很简单的事情,但我对此非常陌生。感谢。

1 个答案:

答案 0 :(得分:0)

尝试替换它:

mysql_query("UPDATE Cars SET Make='$Make', Model='$Model', Year='$Year', Engine='$Engine' WHERE CarID=$CarID'")

到此:

mysql_query("UPDATE `Cars` SET `Make`='$Make', `Model`='$Model', `Year`='$Year', `Engine`='$Engine' WHERE `CarID`=$CarID")

编辑: 您的错误意味着当您输出表单时,还没有帖子。所以php部分:

<?php
  // Connecting to Database Server Code Not Displaying Details For Example

if (isset($_POST)) {
  // If connection cannot be made to Database Server
  if (!$connection)
    die("Cannot connect to Database");

  // Select the database
  mysql_select_db("db1520621", $connection)
    // If Database cannot be found
    or die("Cannot find Database");

  // Check form has been submitted
    $CarID = $_GET['CarID'];

    $Make = $_POST['Make'];
    $Model = $_POST['Model'];
    $Year = $_POST['Year'];
    $Engine = $_POST['Engine'];

    // Save data to database
    mysql_query("UPDATE `Cars` SET `Make`='$Make', `Model`='$Model', `Year`='$Year', `Engine`='$Engine' WHERE `CarID`=$CarID")
    or die(mysql_error());

    echo("$Make $Model was updated");

  // Close connection to Database
  mysql_close($connection);
}
?>