MySQL & PHP 更新问题,使用准备好的语句
MySQL & PHP Update issue, using prepared statements
我被要求和某人一起做一个项目(从代码中猜测行业没有奖品)。虽然我对站点的 HTML 和 CSS 没问题,但这确实是我第一次尝试创建一个管理页面,我可以在其中查看、删除和更新记录。
我的问题来自于尝试更新通过 URL 传递的 ID 链接到的特定记录。当前数据显示正常。当我尝试向字段中添加新数据时,所有声明的变量都得到 'Undefined index' 。根本没有更新数据。
以前,我有一个问题,看起来数据可以很好地更新,但该特定记录的所有字段都显示为空白。
表单标签的 'action' 部分没有附加页面,因为我假设如果脚本在同一页面上发生,它不需要指向任何地方。
删除功能已被禁用。我认为同样的问题也会在那里持续存在,直到它被重新引用。
请对我温柔点
<html>
<?php
// declare connection variables
$host = "localhost";
$username = "root";
$password = "";
$dbname = "tsdb";
$socket = "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock";
// DB Connect
$mysqli = new MySQLi($host, $username, $password, $dbname, ini_get('mysqli.default_port'), $socket) or die ('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
// get URL record 'id'
$id = $_GET['id'];
// get result of record from 'id'
$query = "SELECT * FROM models WHERE id =$id";
$result = $mysqli->query($query);
$row = $result->fetch_array();
$result->free();
?>
<!-- vertical table for record edit -->
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<h3>Edit Details</h3>
<form class="form-group" id="editRecord" action="#" method="post">
<table class="table table-striped">
<tr>
<th scope="row">Record ID:</th>
<td><?php echo $row['id']; ?></td>
</tr>
<tr>
<th scope="row">Display Name:</th>
<td><input type='text' class='form-control' value="<?php echo $row['name']; ?>"></td>
</tr>
<tr>
<th scope="row">Featured:</th>
<td><input type='text' class='form-control' value="<?php echo $row['featured']; ?>"></td>
</tr>
<tr>
<th scope="row">Sex:</th>
<td><input type='text' class='form-control' value="<?php echo $row['sex']; ?>"></td>
</tr>
<tr>
<th scope="row">Age:</th>
<td><input type='text' class='form-control' value="<?php echo $row['age']; ?>"></td>
</tr>
<tr>
<th scope="row">Ethnicity:</th>
<td><input type='text' class='form-control' value="<?php echo $row['ethnicity']; ?>"></td>
</tr>
<tr>
<th scope="row">Contact Number:</th>
<td><input type='text' class='form-control' value="<?php echo $row['number']; ?>"></td>
</tr>
<tr>
<th scope="row">Email:</th>
<td><input type='text' class='form-control' value="<?php echo $row['email']; ?>"></td>
</tr>
<tr>
<th scope="row">Display Email ?:</th>
<td><input type='text' class='form-control' value="<?php echo $row['displayEmail']; ?>"></td>
</tr>
<tr>
<th scope="row">Postcode:</th>
<td><input type='text' class='form-control' value="<?php echo $row['postcode']; ?>"></td>
</tr>
<tr>
<th scope="row">Nearest Station:</th>
<td><input type='text' class='form-control' value="<?php echo $row['station']; ?>"></td>
</tr>
<tr>
<th scope="row">Parking:</th>
<td><input type='text' class='form-control' value="<?php echo $row['parking']; ?>"></td>
</tr>
<tr>
<th scope="row">Shower:</th>
<td><input type='text' class='form-control' value="<?php echo $row['shower']; ?>"></td>
</tr>
<tr>
<th scope="row">Outcalls:</th>
<td><input type='text' class='form-control' value="<?php echo $row['outcalls']; ?>"></td>
</tr>
<tr>
<th scope="row">Price:</th>
<td><input type='text' class='form-control' value="<?php echo $row['price']; ?>"></td>
</tr>
<tr>
<th scope="row">Heading:</th>
<td><input type='text' class='form-control' value="<?php echo $row['heading']; ?>"></td>
</tr>
<tr>
<th scope="row">Bio:</th>
<td><input type='text' class='form-control' value="<?php echo $row['bio']; ?>"></td>
</tr>
<tr>
<th scope="row">Created:</th>
<td><?php echo $row['created']; ?></td>
</tr>
<tr>
<th scope="row">Last Updated:</th>
<td><?php echo $row['updated']; ?></td>
</tr>
</table>
<div class="btn-group">
<button class="btn btn-default" name="update" type="submit">Update Record</button>
<button class="btn btn-default" name="delete" type="submit">Delete Record</button>
</div>
</form>
</div>
</div>
</div>
<?php
// button execute code
if (isset($_POST['update'])) {
$name = $_POST['name'];
$featured = $_POST['featured'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$ethnicity = $_POST['ethnicity'];
$number = $_POST['number'];
$email = $_POST['email'];
$displayEmail = $_POST['displayEmail'];
$postcode = $_POST['postcode'];
$station = $_POST['station'];
$parking = $_POST['parking'];
$shower = $_POST['shower'];
$outcalls = $_POST['outcalls'];
$price = $_POST['price'];
$heading = $_POST['heading'];
$bio = $_POST['bio'];
$updateQuery = "UPDATE models SET name=?, featured=?, sex=?, age=?, ethnicity=?, number=?, email=?, displayEmail=?, postcode=?, station=?, parking=?, shower=?, outcalls=?, price=?, heading=?, bio=? WHERE id = 'id'";
$updatestmt = $mysqli->prepare($updateQuery);
$updatestmt->bind_param('sssissssssssssss', $name, $featured, $sex, $age, $ethnicity, $number, $email, $displayEmail, $postcode, $station, $parking, $shower, $outcalls, $price, $heading, $bio);
$updatestmt->execute();
$updatestmt->close();
}
elseif (isset($_POST['delete'])) {
$deleteAlert = "Feature not available!";
echo "<script type='text/javascript'>alert('$deleteAlert');</script>";
};
$mysqli->close();
?>
</body>
</html>
您没有为输入标签指定名称,例如<input name="featured"
我被要求和某人一起做一个项目(从代码中猜测行业没有奖品)。虽然我对站点的 HTML 和 CSS 没问题,但这确实是我第一次尝试创建一个管理页面,我可以在其中查看、删除和更新记录。
我的问题来自于尝试更新通过 URL 传递的 ID 链接到的特定记录。当前数据显示正常。当我尝试向字段中添加新数据时,所有声明的变量都得到 'Undefined index' 。根本没有更新数据。
以前,我有一个问题,看起来数据可以很好地更新,但该特定记录的所有字段都显示为空白。
表单标签的 'action' 部分没有附加页面,因为我假设如果脚本在同一页面上发生,它不需要指向任何地方。
删除功能已被禁用。我认为同样的问题也会在那里持续存在,直到它被重新引用。
请对我温柔点
<html>
<?php
// declare connection variables
$host = "localhost";
$username = "root";
$password = "";
$dbname = "tsdb";
$socket = "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock";
// DB Connect
$mysqli = new MySQLi($host, $username, $password, $dbname, ini_get('mysqli.default_port'), $socket) or die ('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
// get URL record 'id'
$id = $_GET['id'];
// get result of record from 'id'
$query = "SELECT * FROM models WHERE id =$id";
$result = $mysqli->query($query);
$row = $result->fetch_array();
$result->free();
?>
<!-- vertical table for record edit -->
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<h3>Edit Details</h3>
<form class="form-group" id="editRecord" action="#" method="post">
<table class="table table-striped">
<tr>
<th scope="row">Record ID:</th>
<td><?php echo $row['id']; ?></td>
</tr>
<tr>
<th scope="row">Display Name:</th>
<td><input type='text' class='form-control' value="<?php echo $row['name']; ?>"></td>
</tr>
<tr>
<th scope="row">Featured:</th>
<td><input type='text' class='form-control' value="<?php echo $row['featured']; ?>"></td>
</tr>
<tr>
<th scope="row">Sex:</th>
<td><input type='text' class='form-control' value="<?php echo $row['sex']; ?>"></td>
</tr>
<tr>
<th scope="row">Age:</th>
<td><input type='text' class='form-control' value="<?php echo $row['age']; ?>"></td>
</tr>
<tr>
<th scope="row">Ethnicity:</th>
<td><input type='text' class='form-control' value="<?php echo $row['ethnicity']; ?>"></td>
</tr>
<tr>
<th scope="row">Contact Number:</th>
<td><input type='text' class='form-control' value="<?php echo $row['number']; ?>"></td>
</tr>
<tr>
<th scope="row">Email:</th>
<td><input type='text' class='form-control' value="<?php echo $row['email']; ?>"></td>
</tr>
<tr>
<th scope="row">Display Email ?:</th>
<td><input type='text' class='form-control' value="<?php echo $row['displayEmail']; ?>"></td>
</tr>
<tr>
<th scope="row">Postcode:</th>
<td><input type='text' class='form-control' value="<?php echo $row['postcode']; ?>"></td>
</tr>
<tr>
<th scope="row">Nearest Station:</th>
<td><input type='text' class='form-control' value="<?php echo $row['station']; ?>"></td>
</tr>
<tr>
<th scope="row">Parking:</th>
<td><input type='text' class='form-control' value="<?php echo $row['parking']; ?>"></td>
</tr>
<tr>
<th scope="row">Shower:</th>
<td><input type='text' class='form-control' value="<?php echo $row['shower']; ?>"></td>
</tr>
<tr>
<th scope="row">Outcalls:</th>
<td><input type='text' class='form-control' value="<?php echo $row['outcalls']; ?>"></td>
</tr>
<tr>
<th scope="row">Price:</th>
<td><input type='text' class='form-control' value="<?php echo $row['price']; ?>"></td>
</tr>
<tr>
<th scope="row">Heading:</th>
<td><input type='text' class='form-control' value="<?php echo $row['heading']; ?>"></td>
</tr>
<tr>
<th scope="row">Bio:</th>
<td><input type='text' class='form-control' value="<?php echo $row['bio']; ?>"></td>
</tr>
<tr>
<th scope="row">Created:</th>
<td><?php echo $row['created']; ?></td>
</tr>
<tr>
<th scope="row">Last Updated:</th>
<td><?php echo $row['updated']; ?></td>
</tr>
</table>
<div class="btn-group">
<button class="btn btn-default" name="update" type="submit">Update Record</button>
<button class="btn btn-default" name="delete" type="submit">Delete Record</button>
</div>
</form>
</div>
</div>
</div>
<?php
// button execute code
if (isset($_POST['update'])) {
$name = $_POST['name'];
$featured = $_POST['featured'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$ethnicity = $_POST['ethnicity'];
$number = $_POST['number'];
$email = $_POST['email'];
$displayEmail = $_POST['displayEmail'];
$postcode = $_POST['postcode'];
$station = $_POST['station'];
$parking = $_POST['parking'];
$shower = $_POST['shower'];
$outcalls = $_POST['outcalls'];
$price = $_POST['price'];
$heading = $_POST['heading'];
$bio = $_POST['bio'];
$updateQuery = "UPDATE models SET name=?, featured=?, sex=?, age=?, ethnicity=?, number=?, email=?, displayEmail=?, postcode=?, station=?, parking=?, shower=?, outcalls=?, price=?, heading=?, bio=? WHERE id = 'id'";
$updatestmt = $mysqli->prepare($updateQuery);
$updatestmt->bind_param('sssissssssssssss', $name, $featured, $sex, $age, $ethnicity, $number, $email, $displayEmail, $postcode, $station, $parking, $shower, $outcalls, $price, $heading, $bio);
$updatestmt->execute();
$updatestmt->close();
}
elseif (isset($_POST['delete'])) {
$deleteAlert = "Feature not available!";
echo "<script type='text/javascript'>alert('$deleteAlert');</script>";
};
$mysqli->close();
?>
</body>
</html>
您没有为输入标签指定名称,例如<input name="featured"