I have build a back end system that update text and images. Values will be passed from a URL on an admin.php page to the index page. A table is being used to display the text and image. The updated text and image should be displayed in the table. For some strange reason, the text and image always disappear when I update content and then click on the edit content button. The content is updating in database with no problem . I am not geting any errors. How do I get updated text and image to display in the table without content disappearing? I thank you for your help, I am still learning PHP.
This is the code:
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>