Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 17945

Updating PHP CMS admin erases image

$
0
0

I was wondering if I could please have some help with a PHP issue I'm having?I'm a beginner and I've been working on making a basic CMS as a personal project to build my skills with.It's all working so far, but on the admin page which updates the site, I'm having a problem with the updating part.The page is populated by DB information, and it is updating when I choose to submit the update form. But if I only alter the text and don't re-select an image, or update the form without choosing a new image, the form sends an empty string to the database which erases the image that was there.

Here is the original code for the page:

<?php include("../core/db.php"); ?><?php if(isset($_GET['page'])){    $page=$_GET['page'];} else {    $page="home";}?><?php////Select from DB query$query="(SELECT * FROM pages WHERE page_title='{$page}')";$get_page_query=mysqli_query($connection,$query);if(!$get_page_query){    die("Get page query problem" . mysqli_error($connection));}while($row=mysqli_fetch_assoc($get_page_query)){$page_title=$page;$page_content=$row['page_content'];$page_image=$row['page_image'];}?><!----Navigation----><table border="1"><tr height="50"><td colspan="2"><a href="admin.php?page=home">Home</a> | <a href="admin.php?page=about_us">About us</a> | <a href="admin.php?page=about_our_style">About our style</a> | <a href="admin.php?page=fees">Fees</a> | <a href="admin.php?page=time_table">Time table</a> | <a href="admin.php?page=gallery">Gallery</a> | <a href="admin.php?page=contact">Contact</a></td>        </tr></table><!----Update form----><form method="post" action="" enctype="multipart/form-data"><p>Page Image<br/><input type="file" class="form-control" name="image"></p><img src="../images/<?php echo $page_image; ?>" width="300"><p>Page Content<br/><textarea name="page_content" type="text" rows="10" cols="100"><?php echo $page_content; ?></textarea></p><p><input type="submit" name="update" value="update"/></p></form><?php////Update queryif(isset($_POST['update'])){    echo "Update posted";    $page_content=$_POST['page_content'];    $page_image=$_FILES['image']['name'];    $page_image_tmp=$_FILES['image']['tmp_name'];    move_uploaded_file($page_image_tmp, "../images/$page_image");    $query="UPDATE pages SET page_image='{$page_image}', page_content='{$page_content}' WHERE page_title='{$page}'";    $update_page_query=mysqli_query($connection,$query);     if(!$update_page_query){    die("Update query problem" . mysqli_error($connection));        }header("location:admin.php?page={$page}");}?>I've spent a few hours on this but I can't work out how to fix it.Any help would be greatly appreciated.Thank you for any help.

I've tried writing an if statement to fix it but it doesn't do anything at all:

    if(empty($_FILES['image']['name'])){        $page_image=$row['page_image'];    } else {        $page_image=$_FILES['image']['name'];    $page_image_tmp=$_FILES['image']['tmp_name'];    move_uploaded_file($page_image_tmp, "../images/$page_image");    }

Viewing all articles
Browse latest Browse all 17945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>