Page 1 of 1

CMS Editing problem

Posted: Mon Aug 13, 2007 1:45 am
by w33nie
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I've been recently trying to build a CMS system for a new site of mine, everything's working fine so far. (Adding, Viewing & Deleting items)
but now I'm trying to get the Editing part of the CMS going, the title & content is displayed perfectly in the form, but absolutely nothing happens when I click the submit button. In fact it reverts to the original content.


What I'm trying to accomplish is from this tutorial - http://www.php-mysql-tutorial.com/cms-a ... -mysql.php

note, I've taken the cache out from that tutorial because my site is small and doesnt need it. viewing & deleting items works just fine without the cache.
I've also taken out the library php files it includes (config.php, opendb.php etc.) because the information in those is contained in my code, and once again, the other pages work fine without.

Code: Select all

<?php
session_start();

if (!isset($_SESSION['basic_is_logged_in']) || $_SESSION['basic_is_logged_in'] !== true) {
	// not logged in, move to login page
	header('Location: index.php');
	exit;
}

?>
<?php
$hostname = 'p50mysql1.secureserver.net';
$username = 'bull';
$password = '*****';
$dbname = 'bull';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CMS Donate</title>
<link href="../_include/cms.css" rel="stylesheet" type="text/css" />
</head>
<body>
<center>
<h1>Donate Menu</h1>
<br />
<!-- #BeginLibraryItem "/_library/navCmsTop.lbi" -->
<!-- Start -->
<ul class="navCmsTop">
<li><a href="../cms/admin.php">Home</a></li>
<li><a href="../cms/donate.php">Donate</a></li>
<li><a href="../cms/environment.php">Environment</a></li>
<li><a href="../cms/interest_rates.php">Interest Rates</a></li>
<li><a href="../cms/news.php">News</a></li>
<li><a href="../cms/video.php">Video</a></li>
<li><a href="../cms/work_choices.php">Work Choices</a></li>
<li><a href="../cms/logout.php">Logout</a></li>
</ul>
<!-- End -->
<!-- #EndLibraryItem -->
<div class="content">
<?php
if(isset($_GET['id']))
{
   $query  = "SELECT id, title, content ".
             "FROM `donate` ".
             "WHERE id = '{$_GET['id']}'";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
   list($id, $title, $content) = mysql_fetch_array($result, MYSQL_NUM);

   $content = htmlspecialchars($content);
}
else if(isset($_POST['save']))
{
   $id = $_POST['id'];
   $title = $_POST['title'];
   $content = $_POST['content'];

   if(!get_magic_quotes_gpc())
   {
      $title = addslashes($title);
      $content = addslashes($content);
   }

   // update the article in the database
   $query = "UPDATE `donate` ".
            "SET title = '$title', content = '$content' ".
            "WHERE id = '$id'";
   mysql_query($query) or die('Error : ' . mysql_error());

   echo "<p>Item '$title' updated</p>";

   // now we will display $title & content
   // so strip out any slashes
   $title   = stripslashes($title);
   $content = stripslashes($content);
}
?>
<form method="post">
<input type="hidden" name="id" value="<?=$id;?>">
<table class="view">
<tr> 
<td style="width: 75px;">Title</td>
<td style="width: 425px;"><input name="title" type="text" id="title" value="<?=$title;?>" style="width: 300px;" /></td>
</tr>
<tr> 
<td style="width: 75px;">Content</td>
<td style="width: 425px;"><textarea name="content" cols="68" rows="10" id="content"><?=$content;?></textarea></td>
</tr>
<tr> 
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr> 
<td colspan="2" style="text-align: center;"><input name="update" type="submit" id="update" value=" Update Article "></td>
</tr>
</table>
<br />
<p><a href="donate.php">Back</a></p>
</form>
</div>
</center>
</body>
</html>

note, I've tried this to no success

Code: Select all

<input type="hidden" name="id" value="<?php echo $id;?>">
<table class="view">
<tr> 
<td style="width: 75px;">Title</td>
<td style="width: 425px;"><input name="title" type="text" id="title" value="<?php echo $title;?>" style="width: 300px;" /></td>
</tr>
<tr> 
<td style="width: 75px;">Content</td>
<td style="width: 425px;"><textarea name="content" cols="68" rows="10" id="content"><?php echo $content;?></textarea></td>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Aug 13, 2007 5:14 am
by w33nie
any solutions?

Posted: Mon Aug 13, 2007 5:18 am
by superdezign
There is no magic solution, and a procedural CMS isn't exactly something that we can just "fix." You'll need to be more specific as to where it is failing. If everything works except for editing:
  • Maybe the data isn't being posted
  • Maybe you aren't handling the posted data correctly
  • Maybe the posted data never get's inserted into your SQL
  • Maybe your SQL is incorrect
  • Maybe your conditions for the form being posted are wrong (do you check for the button, or !empty($_POST)?)
  • Maybe you are skipping your UPDATE / REPLACE statement
A number of things could be wrong. Start debugging by checking the posted data (print_r($_POST)), echoing data, following the procedures of your code (with echoing), testing your SQL queries outside of PHP, etc.

Help us help you.

Posted: Mon Aug 13, 2007 9:24 am
by w33nie
okay, i added this just under the body tag,

Code: Select all

<?php
echo "<pre>"; 
print_r($_POST);
echo "</pre>";
?>
when i first loaded the page, it gave me this up the top

Code: Select all

Array
(
)
then when i made the changes, this happened.

Code: Select all

Array
(
    [id] => 1
    [title] => Hello changes
    [content] => This is different
    [update] =>  Update Article 
)
but upon clicking the submit button it also reset the information in the text areas, and made no changes to the table it should be changing.

Posted: Mon Aug 13, 2007 9:34 am
by choppsta
You update code is triggered by:

Code: Select all

else if(isset($_POST['save'])) {}
but as you've pointed out, your POST data is:

Code: Select all

Array
(
    [id] => 1
    [title] => Hello changes
    [content] => This is different
    [update] =>  Update Article
)
I guess you need to change your "if" logic, or your form.

Posted: Tue Aug 14, 2007 5:23 am
by w33nie
i changed

Code: Select all

else if(isset($_POST['save'])) {}
to

Code: Select all

else if(isset($_POST['title'])) {}
and it works fine now,
not what the tutorial says at all, but its working

Posted: Tue Aug 14, 2007 7:02 am
by superdezign
w33nie wrote:not what the tutorial says at all, but its working
You can't trust every tutorial that you read. :P
PHP is very easy to learn, and can make a beginner feel like an expert. Then, they teach beginners' methods as though they were expert advice. There are tons of tutorials that are generally useless, but it's not like we can do anything about it... Just stay skeptical.