我已经构建了一个更新文本和图像的后端系统。值将从admin.php页面上的URL传递到索引页面。正在使用表格来显示文本和图像。更新的文本和图像应显示在表格中。出于某些奇怪的原因,当我更新内容然后单击编辑内容按钮时,文本和图像总是消失。内容在数据库中更新没有问题。我没有发现任何错误。如何在没有内容消失的情况下获取更新的文本和图像以显示在表中?我感谢你的帮助,我还在学习PHP。
这是代码:
admin.php的
<?php
require_once('authorize.php');
?>
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
text-decoration: none;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Remove Thumbnail Administration</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Administration</h2>
<p>Below is a list of all thumbnails. Use this page to remove thumbnails as needed.</p>
<hr />
<?php
require_once('appvars.php');
require_once('connectvars.php');
// Connect to the database
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Retrieve the data from MySQL
$query = "SELECT * FROM table1 ORDER BY name ASC, caption ASC";
//$query = "SELECT * FROM table1 ORDER BY name DESC, caption ASC";
$data = mysqli_query($conn, $query);
/// Loop through the array of data, formatting it as HTML
echo '<table>';
echo '<tr><th>Name</th><th>Caption</th><th>Action</th></tr>';
while ($row = mysqli_fetch_array($data)) {
// Display the thumbnails data
echo '<tr class="scorerow"><td><strong>' . $row['name'] . '</strong></td>';
echo '<td>' . $row['caption'] . '</td>';
//edit link
echo '<td><a href="index.php?id=' . $row['id'] . '&image=' . $row['image1'] . '&name=' . $row['name'] .
'&caption=' . $row['caption'] .
'&video=' . $row['video'] . '">Edit </a>';
echo '</td></tr>';
}
echo '</table>';
echo "<br><br>";
mysqli_close($conn);
?>
</body>
</html>
的index.php
<!DOCTYPE html>
<html>
<head>
<title>Edit Conent</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<style>
.bigger_textbox {
width: 400px;
height: 40px;
}
</style>
</head>
<body>
<h3>Edit Conent</h3>
<?php
require_once('appvars.php');
require_once('connectvars.php');
$vid="";
$vname="";
$vcaption="";
$vvideo="";
$id ="";
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(isset($_POST["button_edit"])){
$id = $_POST["id"];
$name = $_POST['name'];
$caption = $_POST['caption'];
$video = $_POST['video'];
$old_picture = $_POST['old_picture'];
if(!empty($_FILES["new_picture"]["name"])) {
$new_picture = $_FILES["new_picture"]["name"];
$qry = mysqli_query($dbc,"Update table1 Set image1='$new_picture', name='$name', caption='$caption', video='$video' Where id='$id'");
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["new_picture"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["new_picture"]["tmp_name"],$target_file)){
if (isset($old_picture) && ($old_picture != $new_picture)) {
@unlink("images/" . $old_picture);
echo "<h1>image was uploaded</h1>";
}
}
}
else{
$qry = "Update table1 Set name='$name', caption='$caption', video='$video' Where id='$id'";
}
$qryUpdate = mysqli_query($dbc,$qry);
}
else if(isset($_GET["edit"])){
$qry = mysqli_query($dbc,"Select * From table1 Where id='".$_GET["edit"]."'");
while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
$vid=$row["id"];
$old_picture=$row["image1"];
$vname=$row["name"];
$vcaption=$row["caption"];
$vvideo=$row["video"];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit</title>
</head>
<body>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method="post" enctype="multipart/form-data" >
<table>
<tr>
<td>Product ID</td>
<td><input type="text" name="id" value="<?php echo $vid;?>"></td></tr>
<tr>
<td>Name</td>
<td><input type="text" class="bigger_textbox" name="name" value="<?php echo $vname;?>"></td></tr>
<tr><td>Caption</td>
<td><input type="text" class="bigger_textbox" name="caption" value="<?php echo $vcaption;?>"></td></tr>
<tr><td>Video</td>
<td><input type="text" class="bigger_textbox" name="video" value="<?php echo $vvideo;?>"></td></tr>
<input type="hidden" name="old_picture" value="<?php if (!empty($old_picture)) echo $old_picture; ?>" />
<tr><td>Picture</td>
<td><input type="file" name="new_picture" ></td></tr>
<?php if (!empty($old_picture)) {
echo '<img class="profile" src="images/' . $old_picture . '" alt="image" style=width:150px;height:xpx;">';
}
?>
<tr><td colspan="2">
<input type="submit" name="button_edit" value="Edit Content"></td></tr> </table>
</form>
<table border=1>
<tr><th>Name</th><th>Caption</th>
<th>Video</th><th>image</th> <th>Action</th></tr>
<?php
if (isset($_GET["id"])) {
$qry =mysqli_query($dbc, "Select * From table1 Where id='".$_GET["id"]."'");
while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)) {
echo '<tr><td>'.$row["name"].'</td>';
echo '<td>'.$row["caption"].'</td>';
echo '<td>'.$row["video"].'</td>';
echo '<td><img src="images/'.$row["image1"].'" style=width:100px;height:xpx;"/></td>';
echo '<td><a href="?id='.$row["id"].'&edit='.$row["id"]. '&video='.$row["video"].'&image='.$row["image1"].'">Edit</a> </td></tr>';
}
}
?>
</table>
<p><a href="admin.php"><< Back to admin page</a></p>
<br><br><br>
</body>
</html>
答案 0 :(得分:0)
在index.php之上,您将清除变量的值。
试试这个:
<?php
isset($vid) ? $vid : "";
isset($vname) ? $vname : "";
isset($vcaption) ? $vcaption : "";
isset($vvideo) ? $vvideo : "";
isset($id) ? $id : "";
这是一个快速反应;但是,您需要重新组织代码。 将HTML部分分成部分,其中静态部分(页眉和页脚可以包含在一行中)。另外,不要在每一行上使用echo,而是使用$ output。=“结果数据”;连接数据,然后回显$ output。