我有一张表,用户可以选择编辑每一行,这会更新数据库中的行。我没有收到任何关于此代码的错误,它正在返回"已成功更新记录"但实际上没有任何内容在数据库中得到更新。
如果有人能在这里发现问题我会非常感激。
table.php:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Flight Table</title>
<!-- Scripts -->
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="modify_records.js"></script>
<!-- CSS -->
<link href="css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-12">
<table class="table table-striped table-bordered table-hover" id="table">
<thead>
<tr>
<th>Edit</th>
<th>ID</th>
<th>Date</th>
<th>Aircraft</th>
<th>Nature of flight</th>
<th>Authorised By</th>
<th>Duration</th>
</tr>
</thead>
<?php
include 'config.php';
$conn= new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql = "SELECT * FROM tbl_flights;";
$result = $conn->query($sql);
?>
<tbody>
<?php while ($row=$result->fetch_assoc()) { ?>
<tr>
<?php echo "<td align='center'><a href='edit_form.php?flight_id=" . $row['flight_id'] . "'>Edit</a></td>"; ?>
<td>
<?php echo $row['flight_id']; ?>
</td>
<td>
<?php echo $row['flight_date']; ?>
</td>
<td>
<?php echo $row['aircraft_id']; ?>
</td>
<td>
<?php echo $row['flight_nature']; ?>
</td>
<td>
<?php echo $row['auth_by']; ?>
</td>
<td>
<?php echo $row['auth_duration']; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
edit_form.php:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Edit</title>
<!-- Scripts -->
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="modify_records.js"></script>
<!-- CSS -->
<link href="css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>
<?php
if (isset($_GET['flight_id']) && is_numeric($_GET['flight_id'])) {
// get the 'id' variable from the URL
$flight_id = $_GET['flight_id'];
include 'config.php';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM tbl_flights WHERE flight_id = $flight_id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
}
?>
<form action="action.php" method="post">
<p>
<input type="hidden" name="flight_id" id="flight_id" value="<?php echo $row['flight_id']; ?>">
</p>
<p>
<label for="flight_date">Date:</label>
<input type="text" name="flight_date" id="flight_date" value="<?php echo $row['flight_date']; ?>">
</p>
<p>
<label for="aircraft_id">Aircraft:</label>
<input type="text" name="aircraft_id" id="aircraft_id" value="<?php echo $row['aircraft_id']; ?>">
</p>
<p>
<label for="flight_nature">Nature of Flight:</label>
<input type="text" name="flight_nature" id="flight_nature" value="<?php echo $row['flight_nature']; ?>">
</p>
<p>
<label for="auth_by">Auth By:</label>
<input type="text" name="auth_by" id="auth_by" value="<?php echo $row['auth_by']; ?>">
</p>
<p>
<label for="auth_duration">Auth Duration:</label>
<input type="text" name="auth_duration" id="auth_duration" value="<?php echo $row['auth_duration']; ?>">
</p>
<input type="submit" value="Save">
</form>
</body>
</html>
action.php的:
<?php
// database connection
include 'pdo_config.php';
try {
$conn = new PDO($dsn, $user, $pass, $opt);
// post data
$flight_id = $_POST['flight_id'];
$flight_date = $_POST['flight_date'];
$aircraft_id = $_POST['aircraft_id'];
$flight_nature = $_POST['flight_nature'];
$auth_by = $_POST['auth_by'];
$auth_duration = $_POST['auth_duration'];
// prepare statement and bind parameters
$stmt = $conn->prepare("UPDATE tbl_flights
SET (flight_date = :flight_date, aircraft_id = :aircraft_id, flight_nature = :flight_nature, auth_by = :auth_by, auth_duration = :auth_duration)
WHERE flight_id = :flight_id");
$stmt->bindParam(':flight_id', $flight_id);
$stmt->bindParam(':flight_date', $flight_date);
$stmt->bindParam(':aircraft_id', $aircraft_id);
$stmt->bindParam(':flight_nature', $flight_nature);
$stmt->bindParam(':auth_by', $auth_by);
$stmt->bindParam(':auth_duration', $auth_duration);
// execute statement
$stmt->execute();
// success or error message
echo "Updated record successfully.";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
答案 0 :(得分:2)
您的UPDATE语法不正确。 ()
不应该SET
括号:
UPDATE tbl_flights
SET (flight_date = :flight_date,
aircraft_id = :aircraft_id,
flight_nature = :flight_nature,
auth_by = :auth_by,
auth_duration = :auth_duration)
WHERE flight_id = :flight_id
将其更改为以下内容:
UPDATE tbl_flights
SET flight_date = :flight_date,
aircraft_id = :aircraft_id,
flight_nature = :flight_nature,
auth_by = :auth_by,
auth_duration = :auth_duration
WHERE flight_id = :flight_id
根据手册:
手册中的示例:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
如您所见,()
后面没有SET
。