Jump to content

[SOLVED] DB update not working - possibly just needs a 2nd pair of eyes


Recommended Posts

Hi guys,

 

It's been a long time but I decided to start playing with PHP again, I've been writing a CMS for a website i'm making and this page is giving me grief, i've used this script in some of my other websites in the past and its been fine so I'm assuming i've made a typo somewhere but I can't for the life of me find it.

 

No matter what I do I get the 'all fields must be filled' message pop up, if I remove the validation then I get the success message but the DB doesnt update!

 

here is the code:

 

if(isset($_GET['ID']))
{
// Pulls the data from the database
$dblookup = "SELECT pageID, pageName, articleTitle, articleBody, meta_key, meta_desc FROM core_content WHERE pageID=" . $_GET['ID'];
$data = mysql_query($dblookup) or die('Failed to return data: ' . mysql_error());
/* sorts the data into variables and puts them in an array ready to be called when needed */
list($pageID, $pageName, $articleTitle, $articleBody, $meta_key, $meta_desc) = mysql_fetch_array($data, MYSQL_NUM);
}
elseif (!empty($_POST['submit']))
{
	if (empty($articleTitle) || empty($articleBody) || empty($pageName) || empty($meta_key) || empty($meta_desc)) 
	{
		$message = '<strong><p class="error">All fields MUST be filled</p></strong>';

		$pageID 	 	= $_POST['pageID'];
		$articleTitle   = $_POST['articleTitle'];
		$articleBody 	= $_POST['articleBody'];
		$pageName	 	= $_POST['pageName'];
		$meta_key	 	= $_POST['meta_key'];
		$meta_desc	 	= $_POST['meta_desc'];
	} 
	else 
	{
		$pageID 	 	= $_POST['pageID'];
		$articleTitle   = $_POST['articleTitle'];
		$articleBody 	= $_POST['articleBody'];
		$pageName	 	= $_POST['pageName'];
		$meta_key	 	= $_POST['meta_key'];
		$meta_desc	 	= $_POST['meta_desc'];
		// Updates the database
		$dbupdate = "UPDATE core_content ".
         "SET articleTitle = '$articleTitle', pageID = '$pageID', articleBody = '$articleBody', pageName = '$pageName', meta_key = '$meta_key', meta_desc = '$meta_desc'".
		 "WHERE pageID = '$pageID'";
		mysql_query($dbupdate) or die('<h3 class="error"> Update Failed! </h3>' . mysql_error());

		// Return a message if successful.
		$message = '<strong><p class="success">Page updated</p></strong>';
	}

}
?>
          <h3> <?php echo "Curently Editing: " . $pageName ;?></h3>
          <h3>
          <?php echo $message; ?>
          <p class="error"> WARNING: This will modify core pages on the website, please ensure you have the appropriate permissions to perform this task, errors can cause the site to fail.</p>
          <form enctype="multipart/form-data" action="pages_edit.php" method="post">
            <input name="pageID" type="hidden" id="pageID" size="3" value="<?php echo $pageID ?>">
            <p>
              <label>Short Headline<br />
                <input name="pageName" type="text" id="pageName" size="50" value="<?php echo $pageName ?>">
              </label>
            </p>
            <p>
              <label>Full Headline<br />
                <input name="articleTitle" type="text" id="articleTitle" size="50" value="<?php echo $articleTitle ?>">
              </label>
            </p>
            <p>
              <label>Body Text<br />
                <textarea name="articleBody" id="articleBody" cols="50" rows="15"><?php echo $articleBody ?></textarea>
              </label>
            </p>
            <p>
              <label>Keywords (At least 5 - separeted by commas)
                <textarea name="meta_key" type="text" id="meta_key" cols="50" rows="3"><?php echo $meta_key ?></textarea>
              </label>
            </p>
            <p>
              <label>Article Summary<br />
                <textarea name="meta_desc" type="text" id="meta_desc" cols="50" rows="5"><?php echo $meta_desc ?></textarea>
              </label>
            </p>
            <input name="submit" type="submit" value="Submit">
          </form>

You logic won't work because you're not populating the $pageID, $pageName, $articleTitle, $articleBody, $meta_key, $meta_desc variables in the elseif part of your statement.

 

You're doing

if (blah) populate variables (listed above)

elseif(another blah) use variables populated... OOPS i haven't populated them in this part...

Yup they do come from the $_POST, but you're using them before you've populated from $_POST. see line:

if (empty($articleTitle) || empty($articleBody) || empty($pageName) || empty($meta_key) || empty($meta_desc))

 

You've not set those variables yet ;)

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.