2016-11-23 53 views
1

我有一張表,用戶可以選擇編輯每行,更新數據庫中的行。我沒有得到這個代碼的任何錯誤,它返回「更新記錄成功」,但實際上沒有在數據庫中更新。PHP MySQL更新表格行並非實際更新,沒有錯誤

如果有人能在這裏找到問題,我將不勝感激。

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; 
+2

這不是一個有效的UPDATE SQL語句。請參閱[docs](http://dev.mysql.com/doc/refman/5.7/en/update.html)以獲取正確的語法。 –

+0

回想起來,UPDATE語法應該是UPDATE SET = , = ,where WHERE ;' – FDavidov

+0

任何你正在混合mysqli和PDO的原因?請注意,您在mysqli部分有一個sql注入問題:'$ _GET ['flight_id']' – jeroen

回答

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