晚上好!我试图通过我的管理员面板更新数据库中的记录,但不知道该怎么做,或者可能是我的逻辑不正确。这是我的代码。
更新查询无效。
<?php
include 'include/dbConnection.php';
?>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$title = $_GET['Book_Title'];
$edition = $_GET['Book_Edition'];
$author = $_GET['Author_Name'];
$pubication = $_GET['Book_Publication'];
$copies = $_GET['Book_No_Of_Copies'];
$price = $_GET['Book_Price'];
$query = "SELECT * FROM book";
$run = mysqli_query($query);
foreach ($run as $key) { ?>
<form method="GET" action="updateBook.php">
<input type="text" name="title" value="<?php echo $key['Book_Title']; ?>" />
<input typt="text" name="edition" value="<?php echo $key['Book_Edition']; ?>" />
<input typt="text" name="author" value="<?php echo $key['Author_Name']; ?>" />
<input typt="text" name="pubication" value="<?php echo $key['Book_Publication']; ?>" />
<input typt="text" name="copies" value="<?php echo $key['Book_No_Of_Copies']; ?>" />
<input typt="text" name="price" value="<?php echo $key['Book_Price']; ?>" />
<input type="submit" name="submit" />
</form>
<?php }
$sql = "UPDATE book SET Book_Title='$title',Book_Edition='$edition',Author_Name='$author',
Book_Publication='$pubication',Book_No_Of_Copies='$copies',Book_Price='$price'
WHERE Book_ID='$id'";
if (mysqli_query($conn, $sql)) {
header("location: viewBook.php");
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
<?php
}
$sql = "UPDATE book SET Book_Title='$title',Book_Edition='$edition',Author_Name='$author', Book_Publication='$pubication',Book_No_Of_Copies='$copies',Book_Price='$price' WHERE Book_ID='$id'";
if (mysqli_query($conn, $sql)) {
header("location: viewBook.php");
}
else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
我已经添加了你的php代码。如果需要,请做一些修正。并详细描述您的代码和您的问题。
答案 0 :(得分:0)
$query = "SELECT * FROM book where id='$id'";
$result = mysqli_query($conn, $query);
$key = mysqli_fetch_assoc($result);
?>
在您的视图中填充数据
<form method="GET" action="updateBook.php">
<input type="text" name="title" value="<?php echo $key['Book_Title']; ?>" />
<input typt="text" name="edition" value="<?php echo $key['Book_Edition']; ?>" />
<input typt="text" name="author" value="<?php echo $key['Author_Name']; ?>" />
<input typt="text" name="pubication" value="<?php echo $key['Book_Publication']; ?>" />
<input typt="text" name="copies" value="<?php echo $key['Book_No_Of_Copies']; ?>" />
<input typt="text" name="price" value="<?php echo $key['Book_Price']; ?>" />
<input type="submit" name="submit" />
</form>