2017-07-30 27 views
0

我不知道這是什麼錯誤,沒有錯誤,但它沒有處理更新查詢。我不知道它的最新代碼或舊代碼。第一個代碼是accommodation_server.php。有沒有錯誤,但它不適用於PHP和MySQL中的UPDATE查詢

這裏的結構:

<?php 

    session_start(); 

    $title_room = ""; 
    $room_size = ""; 
    $occupancy = ""; 
    $bed = ""; 
    $rate = ""; 
    $others = ""; 
    $others1 = ""; 
    $others2 = ""; 
    $others3 = ""; 
    $others4 = ""; 
    $id = 0; 
    $edit_state = false; 

    //connect to the database 
    $db = mysqli_connect('localhost', 'root', '', 'accommodation'); 

    //update 
    if (isset($_POST['update'])) { 
     $title_room = $_POST['text_title']; 
     $room_size = $_POST['text_size']; 
     $occupancy = $_POST['text_occupancy']; 
     $rate = $_POST['text_rate']; 
     $others = $_POST['text_others1']; 
     $others1 = $_POST['text_others2']; 
     $others2 = $_POST['text_others3']; 
     $others3 = $_POST['text_others4']; 
     $others4 = $_POST['text_others5']; 
     $bed = $_POST['text_bed']; 
     $description = $_POST['text_description']; 
     $id = $_POST['text_id']; 

     mysqli_query($db, "UPDATE rooms SET title_room = '$title_room', room_size = '$room_size', occupancy = '$occupancy', rate = '$rate', bed = '$bed', others = '$others', others1 = 'others1', others2 = '$others2', others3 = '$others3', others4 = '$others4', description = '$description' where id=$id"); 
     $_SESSION['msg'] = "Room Updated!"; 
     header('location: accommodation.php'); 
    } 

     //retrieve records 
     $results = mysqli_query($db, "SELECT * FROM rooms"); 
?> 

,這是accommodation.php

<?php include ('accomodation_server.php'); 

    //fetch the record 
    if (isset($_GET['edit'])) { 
     $id = $_GET['edit']; 
     $edit_state = true; 
     $rec = mysqli_query($db, "SELECT * FROM rooms where id=$id"); 
     $record = mysqli_fetch_array($rec); 
     $title_room = $record['title_room']; 
     $room_size = $record['room_size']; 
     $occupancy = $record['occupancy']; 
     $rate = $record['rate']; 
     $others = $record['others']; 
     $others1 =$record['others1']; 
     $others2 = $record['others2']; 
     $others3 = $record['others3']; 
     $others4 = $record['others4']; 
     $bed = $record['Bed']; 
     $description = $record['description']; 
     $id = $record['id']; 
    } 
    ?> 
<html> 
<head> 
    <title>C M S</title> 
    <link rel="stylesheet" type="text/css" href="css/accommodation.css"> 
    <meta name="viewport" content="width=device-width, initial-scale: 1.0, user-scalable=0"/> 
</head> 
<body> 
    <?php if(isset($_SESSION['msg'])): ?> 
     <div class="msg"> 
      <?php 
      echo $_SESSION['msg']; 
      unset($_SESSION['msg']); 
      ?> 
     </div> 
    <?php endif ?> 

    <div id="container"> 
     <div class="sidebar"> 
     <ul id="nav"> 
      <li><a href="dashboard.php">Dashboard</a></li> 
      <li><a class="selected" href="cms.php">CMS</a></li> 
      <li><a href="acc-settings.php">Account Settings</a></li> 
      <li><a href="login.php">Logout</a></li> 

     </ul> 
     </div> 
     <div class="content"> 
     <br> 
     <br> 
     <h1>Accommodation</h1> 
     <p>Edit and remove images or description.</p> 

      <?php while ($row = mysqli_fetch_array($results)) { ?> 

      <form method="post" action="#"> 
      <input type="hidden" name="id" value="<?php echo $id; ?>"> 
      <div id="box"> 
      <div class="title-room"><b> <!--title ng room--> <?php echo $row['title_room']; ?></b></a> 
      <a href="accommodation.php?edit=<?php echo $row['id']; ?>" class = "edit_btn" name="title_edit">Edit</a> 
      <input type="text" name="text_title" placeholder = "Enter the name of the room..." class="title" value="<?php echo $title_room; ?>"> 
      </div> 

      <div class="box-top">Room Size:<p><?php echo $row['room_size']; ?></p></a> 
      <input type="text" name="text_size" placeholder = "Enter the size of the room..." class="size" value="<?php echo $room_size; ?>"> 
      </div> 

      <div class="box-top">Bed: <p><?php echo $row['Bed']; ?></p></a> 
      <input type="text" name="text_bed" placeholder = "Enter the bedtype of the room..." class="bed" value="<?php echo $bed; ?>"> 
      </div> 

      <div class="box-top">Occupancy: <p><?php echo $row['occupancy']; ?></p></a> 
      <input type="text" name="text_occupancy" placeholder = "Enter the occupancy of the room..." class="occupancy" value="<?php echo $occupancy; ?>"> 
      </div> 

      <div class="box-top">Rate from: <p><?php echo $row['rate']; ?></p></a> 
      <input type="text" name="text_rate" placeholder = "Enter the rate of the room..." class="rate" value="<?php echo $rate; ?>"> 
      </div> 

      <div class="box-top">Others: 

      <p class="others1"> <br> 
      <input type="text" name="text_others1" placeholder = "Others..." class="t_others1" value="<?php echo $others; ?>"> 
      <?php echo $row['others']; ?></p> <br> 

      <p class="others2"> 
      <input type="text" name="text_others2" placeholder = "Others..." class="t_others2" value="<?php echo $others1; ?>"> 
      <?php echo $row['others1']; ?></p> <br> 

      <p class="others3"> 
      <input type="text" name="text_others3" placeholder = "Others..." class="t_others3" value="<?php echo $others2; ?>"> 
      <?php echo $row['others2']; ?></p><br> 

      <p class="others4"> 
      <input type="text" name="text_others4" placeholder = "Others..." class="t_others4" value="<?php echo $others3; ?>"> 
      <?php echo $row['others3']; ?></p> <br> 

      <p class="others5"> 
      <input type="text" name="text_others5" placeholder = "Others..." class="t_others5" value="<?php echo $others4; ?>"> 
      <?php echo $row['others4']; ?></p></a><br> </div> 
      <div class="box-panel"> 
       <?php echo $row['description']; ?> 
       <br><p> &nbsp; </p> 
       <textarea placeholder = "Enter the Description of the Room..." rows="4" cols="100" class="description" name="text_description" value="<?php echo $description; ?>"> 
       </textarea> 
      </div> 

      <?php if ($edit_state == false): ?> 
      <button type="submit" name="edit_button" class="edit_button">Edit</button> 
      <?php else: ?> 
      <button type="submit" name="update" class="btn">Update</button> 
      <?php endif ?> 
      </form> 

      <?php } ?> 

      <div id="box"> 
      <div class="box-top"><a href="#">Destination</a></div> 
      <div class="box-panel"> 
       Information of the resort (contact number, destination, etc) 
      </div> 

      <div id="box"> 
      <div class="box-top"><a href="#">Accommodation</a></div> 
      <div class="box-panel"> 
       Rooms, and the description of the room. 
      </div> 

      <div id="box"> 
      <div class="box-top"><a href="#">Photos</a></div> 
      <div class="box-panel"> 
       All images of the website. 
      </div> 



     </div> 
     </div> 
    </div> 
     <div id="header"> 
     <div class="logo"><a href="#">Pacific Sky Beach Resort Admin Panel <span></span></a></div> 
    </div> 
</body> 
</html> 
+0

使用參數化查詢,檢查mysqli_query'的'回報。這是按照SQL注入開放的。 – chris85

+0

什麼意思是「它不工作」?什麼顯示屏幕?任何消息?嘗試使用'echo'test';'快速排除故障。 – GePu

+0

沒有錯誤顯示,但它不工作.. –

回答

-1
mysqli_query($db, "UPDATE rooms SET title_room = '$title_room', room_size = '$room_size', occupancy = '$occupancy', rate = '$rate', bed = '$bed', others = '$others', others1 = 'others1', others2 = '$others2', others3 = '$others3', others4 = '$others4', description = '$description' where id='$id'"); 

你錯過了第一個代碼示例

在'周圍的id = '$ ID'

並再次在第二個

$rec = mysqli_query($db, "SELECT * FROM rooms where id='$id'"); 

,如果你還查詢後加上的print_r($ DB)是它會告訴你,如果有您的SQL請求任何錯誤

+0

在字符串中使用PHP變量時不使用省略號。 – GePu

+0

但我認爲這是一個int。 –

+0

$ id將是一個int tho?據我所知,整個包裹都不會被包裹 – Solor

相關問題