j05hr Posted June 8, 2010 Share Posted June 8, 2010 I have a CMS where you can add new pages and edit existing pages, it all works fine apart from the file upload bit to edit. <p>Position: <select name="position"> <?php $subject_set = get_all_subjects(); $subject_count = mysql_num_rows($subject_set); // $subject_count + 1 b/c we are adding a subject for($count=1; $count <= $subject_count+1; $count++) { echo "<option value=\"{$count}\""; if ($sel_subject['position'] == $count) { echo " selected"; } echo ">{$count}</option>"; } ?> </select> </p> <br /> <p>Visible: <input type="radio" name="visible" value="0"<?php if ($sel_subject['visible'] == 0) { echo " checked"; } ?> /> No <input type="radio" name="visible" value="1"<?php if ($sel_subject['visible'] == 1) { echo " checked"; } ?> /> Yes </p> <br /> <p>Content:<br /> <textarea name="content" rows="10" cols="75"><?php echo $sel_subject['content']; ?></textarea> </p> <br /> <input name="image" accept="image/jpeg" type="file"> <?php echo $sel_subject['image']; ?> <br /> <br /> <input type="submit" name="submit" value="Edit Subject" /> <a href="delete_subject.php?subj=<?php echo urlencode($sel_subject['id']);?>"onclick="return confirm('Are you sure?');">Delete Subject</a> <br /> <br /> <a href="content.php">Cancel</a> </form> How can I make it so the picture already selected will be selected in the iinput="image" box? Quote Link to comment https://forums.phpfreaks.com/topic/204173-editing-a-page/ Share on other sites More sharing options...
Adam Posted June 8, 2010 Share Posted June 8, 2010 The best thing to do here is just leave it blank, and instruct the user to only select an image if they want to overwrite it. The reasons for this are that, for one, when a file is uploaded to the server you only get the file and path to it's *temporary* location on the server; you don't get their local path. This means that you're unable to pre-fill the input with their local path, however even if you could it would mean re-uploading the file every time the form is submitted -- waste of bandwidth! Quote Link to comment https://forums.phpfreaks.com/topic/204173-editing-a-page/#findComment-1069381 Share on other sites More sharing options...
j05hr Posted June 8, 2010 Author Share Posted June 8, 2010 Thanks for that reply, it made sense. I've just realised another problem, say if I was updating a text field in the same form as the images unless you reselect the same image, it will delete the image because nothing is selected so you have to reapply the image every time you update something. How can you work around this? Quote Link to comment https://forums.phpfreaks.com/topic/204173-editing-a-page/#findComment-1069397 Share on other sites More sharing options...
Adam Posted June 8, 2010 Share Posted June 8, 2010 Can't really say without seeing your code, but it sounds like a bad implementation. You should put in a condition that will only overwrite the image if a file has been specified. Quote Link to comment https://forums.phpfreaks.com/topic/204173-editing-a-page/#findComment-1069399 Share on other sites More sharing options...
j05hr Posted June 8, 2010 Author Share Posted June 8, 2010 This is my edit page <?php require_once("includes/session.php"); ?> <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php confirm_logged_in(); ?> <?php if (intval($_GET['subj']) == 0) { redirect_to("content.php"); } if (isset($_POST['submit'])) { $errors = array(); $required_fields = array('menu_name', 'position', 'visible', 'content'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { $errors[] = $fieldname; } } $fields_with_lengths = array('menu_name' => 30); foreach($fields_with_lengths as $fieldname => $maxlength ) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; } } if (empty($errors)) { // Perform Update $id = mysql_prep($_GET['subj']); $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); $content = mysql_prep($_POST['content']); $image = mysql_prep($_POST['image']); $banner = mysql_prep($_POST['banner']); $query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible}, content = '{$content}', image = '{$image}', banner = '{$banner}' WHERE id = {$id}"; $result = mysql_query($query, $connection); if (mysql_affected_rows() == 1) { // Success $message = "The subject was successfully updated."; } else { // Failed $message = "The subject update failed."; $message .= "<br />". mysql_error(); } } else { // Errors occurred $message = "There were " . count($errors) . " errors in the form."; } } // end: if (isset($_POST['submit'])) ?> <?php find_selected_page(); ?> <?php include("includes/header.php"); ?> <div id="nav"> <?php echo navigation($sel_subject, $sel_page); ?><a href="content.php""onclick="return confirm('Warning: Anything you have edited will not save if you click ok');">Back to Menu </a> </div> <div id="banner"> </div> <div id="line"> </div> <div id="content"> <h3>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h3> <br /> <form action="edit_subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post" class="editContent"> <br /> <?php if (!empty($message)) { echo "<p class=\"message\">" . $message . "</p>"; } ?> <?php // output a list of the fields that had errors if (!empty($errors)) { echo "<p class=\"errors\">"; echo "Please review the following fields:<br />"; foreach($errors as $error) { echo " - " . $error . "<br />"; } echo "</p>"; } ?> <br /> <p>Subject name: <input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" id="menu_name" /> </p> <br /> <p>Header Image: <input name="banner" accept="image/jpeg" type="file"> <br /> <br /> <p>Position: <select name="position"> <?php $subject_set = get_all_subjects(); $subject_count = mysql_num_rows($subject_set); // $subject_count + 1 b/c we are adding a subject for($count=1; $count <= $subject_count+1; $count++) { echo "<option value=\"{$count}\""; if ($sel_subject['position'] == $count) { echo " selected"; } echo ">{$count}</option>"; } ?> </select> </p> <br /> <p>Visible: <input type="radio" name="visible" value="0"<?php if ($sel_subject['visible'] == 0) { echo " checked"; } ?> /> No <input type="radio" name="visible" value="1"<?php if ($sel_subject['visible'] == 1) { echo " checked"; } ?> /> Yes </p> <br /> <p>Content:<br /> <textarea name="content" rows="10" cols="75"><?php echo $sel_subject['content']; ?></textarea> </p> <br /> <p>Right Sided Image: <input name="image" accept="image/jpeg" type="file"> <br /> <br /> <input type="submit" name="submit" value="Edit Subject" /> <br /> <br /> <a href="delete_subject.php?subj=<?php echo urlencode($sel_subject['id']);?>"onclick="return confirm('Are you sure?');">Delete Subject</a> <br /> <br /> <a href="content.php">Cancel</a> </form> </div> <?php require("includes/footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/204173-editing-a-page/#findComment-1069402 Share on other sites More sharing options...
Adam Posted June 8, 2010 Share Posted June 8, 2010 Hmm. Contradictory to what I said before that will actually POST their local path, it won't upload the image however. What exactly is the desired result (image path or actual image)? Quote Link to comment https://forums.phpfreaks.com/topic/204173-editing-a-page/#findComment-1069404 Share on other sites More sharing options...
j05hr Posted June 8, 2010 Author Share Posted June 8, 2010 Yeah sorry I don't need them to upload an image as it's an edit page although they might want to edit a new image in? I have the upload image in create_subject.php <?php require_once("includes/session.php"); ?> <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php confirm_logged_in(); ?> <?php $errors = array(); // Form Validation $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { $errors[] = $fieldname; } } $fields_with_lengths = array('menu_name' => 30); foreach($fields_with_lengths as $fieldname => $maxlength ) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; } } if (!empty($errors)) { redirect_to("new_subject.php"); } ?> <?php $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); $content = mysql_prep($_POST['content']); $image = mysql_prep($_POST['image']); $banner = mysql_prep($_POST['banner']); if (is_uploaded_file ($_FILES['image']['tmp_name'])) { echo '<p>The file has been uploaded</p>'; $temp = 'uploads/'.$_FILES['image']['name']; //$temp = 'uploads/fred.jpg'; echo $temp; if (move_uploaded_file ($_FILES['image']['tmp_name'], $temp)) { echo '<p>The file has been moved</p>'; } else { $errors[] = 'The file could not be moved'; $temp = $_FILES['image']['tmp_name']; echo 'failed'; } $image = $temp; } else { $errors[] = 'No file was uploaded'; } //banner //banner if (is_uploaded_file ($_FILES['banner']['tmp_name'])) { echo '<p>The file has been uploaded</p>'; $test = 'uploads/'.$_FILES['banner']['name']; //$temp = 'uploads/fred.jpg'; echo $temp; if (move_uploaded_file ($_FILES['banner']['tmp_name'], $test)) { echo '<p>The file has been moved</p>'; } else { $errors[] = 'The file could not be moved'; $test = $_FILES['banner']['tmp_name']; echo 'failed'; } $banner = $test; } else { $errors[] = 'No file was uploaded'; } //banner //banner ?> <?php $query = "INSERT INTO subjects ( menu_name, position, visible, content, image, banner ) VALUES ( '{$menu_name}', {$position}, {$visible}, '{$content}', '{$image}', '{$banner}' )"; $result = mysql_query($query, $connection); if($result) { // Succes! header("Location: content.php"); //if (is_uploaded_file ($_FILES['image']['tmp_name'])) { //echo '<p>The file has been uploaded</p>'; //} //else { //echo 'nothing uploaded'; //} exit; } else { // Display error message. echo "<p>Subject creation failed.</p>"; echo "<p>" . mysql_error() . "</p>"; } ?> The desired result will just be so when say you edit the menu_name but don't want to touch the picture the picture will still be there. I guess I eventually will want to be able to upload new images to the edit page. Thanks for all your help so far. Quote Link to comment https://forums.phpfreaks.com/topic/204173-editing-a-page/#findComment-1069408 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.