Jump to content

Editing a page


j05hr

Recommended Posts

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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"); ?>

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.