我不知道这有什么不对,没有错误,但它没有解决更新查询。我不知道它是最新代码还是旧代码。第一个代码是accommodation_server.php。
这是结构:
<?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> </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 :(得分:-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'");
你错过了&#39;&#39;周围的id =&#39; $ id&#39;在第一个代码示例中
再次在第二次
$rec = mysqli_query($db, "SELECT * FROM rooms where id='$id'");
如果你在查询后添加print_r($ db),它会告诉你sql请求中是否有任何错误