Page 1 of 1

CMS - Editing cat works, editing post not

Posted: Wed Jul 13, 2011 9:49 am
by Linkjuice57
Hi,

I've been following this YouTube tutorial, and up to the end it works great. I've made a CMS that can display the posts, delete the posts, display categories, add categories and edit categories.

The tutorial ends there, so I had to improvise and create the edit post function myself. It didnt work. I've got some problems with 'header already send out' but i'll get to that later :p. My edit.php (html -> view source) looks like this:
<tr><td>10 tips om uw maandlasten te verlagen </td><td>Author</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent at odio sem. Quisque varius imperdiet augue, vitae pretium libero dapibus vitae. Nulla facilisi. In porta aliquam ante, et interdum ligula ultricies eget. Donec mollis blandit diam sed malesuada. Nunc eleifend ultrices dolor, vel placerat mauris consequat mollis. Curabitur semper, elit in luctus lobortis, dui dui ullamcorper eros, vel ultricies est justo ut massa. Aenean in sapien ligula. Suspendisse massa magna, rutrum id vehicula eget, lobortis a mi. Praesent molestie porttitor felis sit amet dapibus.



Cras id dui felis, et faucibus metus. Cras nec augue nunc. Praesent tincidunt, leo vitae viverra rhoncus, nunc justo scelerisque orci, sit amet fermentum est sem laoreet urna. Nam aliquam odio nec tellus euismod vehicula. Sed porttitor, orci nec scelerisque laoreet, sem massa commodo ante, vitae egestas libero arcu eu augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ut varius turpis. Vestibulum dignissim elit pellentesque elit rhoncus feugiat. Nam urna massa, mollis ac ultrices quis, venenatis in magna. Mauris eget mattis dolor. Phasellus eu velit risus, ut volutpat sapien. Ut rutrum porta eleifend. Curabitur et fringilla risus. Nam a augue at enim malesuada dapibus consequat vel magna. In ultricies nibh eu sapien hendrerit faucibus gravida purus aliquet.</td><td><a href="edit.php?id=8">Edit</a> <a href="delete.php?id=8">Delete</a></td></tr><html> 
<head> 
<title>CMS</title> 
</head> 
 
<body> 
 
<form action="doEdit.php" method="post"> 
	<table> 
    	<tr> 
        	<td><label for="PostName">Name</label></td> 
            <td><input type="text" name="PostName" value="" /></td> 
        </tr> 
        <tr> 
        	<td><label for="PostAuthor">Author</label></td> 
            <td><input type="text" name="PostAuthor" value="" /></td> 
        </tr> 
        <tr> 
        	<td><label for="PostContent">Content</label></td> 
            <td><input type="text" name="PostContent" value="" /></td> 
        </tr> 
        <tr> 
        	<td colspan="2"><input type="submit" name="submit" /></td> 
            <td><input type="hidden" name="id" value="8" /></td> 
        </tr> 
    </table> 
</form> 
 
</body> 
 
</html> 
Notice how the value="" is empty, and the loop for posts.php page is echoed.

And this is the actual PHP code: (edit.php)
<?php
session_start();
include ('inc/functions.php');
if(isset($_SESSION['user'])) {

$post2 = getPosts($_GET['id']);	
	
?>
<html>
<head>
<title>CMS</title>
</head>

<body>

<form action="doEdit.php" method="post">
	<table>
    	<tr>
        	<td><label for="PostName">Name</label></td>
            <td><input type="text" name="PostName" value="<?php echo $post2['Title']; ?>" /></td>
        </tr>
        <tr>
        	<td><label for="PostAuthor">Author</label></td>
            <td><input type="text" name="PostAuthor" value="<?php echo $post2['Author']; ?>" /></td>
        </tr>
        <tr>
        	<td><label for="PostContent">Content</label></td>
            <td><input type="text" name="PostContent" value="<?php echo $post2['Content']; ?>" /></td>
        </tr>
        <tr>
        	<td colspan="2"><input type="submit" name="submit" /></td>
            <td><input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" /></td>
        </tr>
    </table>
</form>

<?php
} else {
	echo "Login error";
}
?>
</body>

</html>
DoEdit.php
<?php
include ('inc/functions.php'); 

if(isset($_POST['submit'])) {
	if(isset($_POST['PostName'])) {
		editPost($_POST['PostName'],$_POST['PostAuthor'],$_POST['PostContent'], $_POST['id']);
		echo "Succes!";
		//header("Location: posts.php");
	} else {
		echo "Please set a category name";
		//include('addCat.php');
	}
} else {
	echo "Something went wrong with doEdit.php";
}
?>
Functions.php
<?php 

// Tijdelijk - show ook warning/notices
error_reporting(E_ALL);

include ('connect.php'); 

function getPosts() {
	$query = mysql_query("SELECT * FROM posts") or die(mysql_error());
	if(mysql_num_rows($query) == 0) {
		echo "<tr><td colspan=\"3\">No Posts were found</td></tr>";
	} else {
	while($post = mysql_fetch_assoc($query)) {	
		echo "<tr><td>";
		echo $post['Title'];
		echo "</td><td>";
		echo $post['Author'];
		echo "</td><td>";
		echo $post['Content'];
		echo "</td><td>";
		echo "<a href=\"edit.php?id=". $post['ID'] . "\">Edit</a> ";
		echo "<a href=\"delete.php?id=". $post['ID'] . "\">Delete</a>";
		echo "</td></tr>";
											}
			}
}

function deletePost($id) {
	$id = (int) $id;
	mysql_query("DELETE FROM posts WHERE ID = '$id'") or die (mysql_error());
	//header("Location: posts.php");
	echo "Succes!";
} 

function editPost($pName, $pAuthor, $pContent, $id) {
	$id = (int) $id;
	$query = mysql_query("UPDATE posts SET Title = '$pName', Author = '$pAuthor', Content = '$pContent' WHERE ID = '$id'") or die (mysql_error());
	echo "Succes! ";
	//header("Location: posts.php");	
}

function getCats() {
	$query = mysql_query("SELECT * FROM categories") or die(mysql_error());
	if(mysql_num_rows($query) == 0) {
		echo "<tr><td colspan=\"3\">No categories were found</td></tr>";
	} else {
	while($cat = mysql_fetch_assoc($query)) {	
		echo "<tr><td>";
		echo $cat['Title'];
		echo "</td><td>";
		echo $cat['Description'];
		echo "</td><td>";
		echo "<a href=\"editCat.php?id=". $cat['ID'] . "\">Edit</a> ";
		echo "<a href=\"deleteCat.php?id=". $cat['ID'] . "\">Delete</a>";
		echo "</td></tr>";
											}
			}
}

function addCat($cName, $cDesc) {
	$query = mysql_query("INSERT INTO categories VALUES(null, '$cName', '$cDesc')") or die (mysql_error());
}

function deleteCat($id) {
	$id = (int) $id;
	mysql_query("DELETE FROM categories WHERE ID = '$id'") or die (mysql_error());
	echo "Succes! ";
	//header("Location: categories.php");
}

function getCat($id) {
	$id = (int) $id;
	$query = mysql_query("SELECT * FROM categories WHERE ID = '$id'") or die (mysql_error());
	return mysql_fetch_assoc($query);
}

function editCat($cName, $cDesc, $id) {
	$id = (int) $id;
	$query = mysql_query("UPDATE categories SET Title = '$cName', Description = '$cDesc' WHERE ID = '$id'") or die (mysql_error());
	echo "Succes! ";
	//header("Location: categories.php");	
}
?>

Can anyone help me with this?

Re: CMS - Editing cat works, editing post not

Posted: Wed Jul 13, 2011 9:55 am
by conradk
Add this to the very top of every file and tell us what errors you see:
ini_set('display_errors',1);
error_reporting(E_ALL);
I won't redo the whole tutorial for you ;) But if you see errors, I can help you work them out.

Re: CMS - Editing cat works, editing post not

Posted: Wed Jul 13, 2011 10:31 am
by Linkjuice57
Hi, thanks for your fast reply!

I've got that in my functions.php, which is included into every file :-)

There is no actual errors, just doesnt work.

Re: CMS - Editing cat works, editing post not

Posted: Wed Jul 13, 2011 10:49 am
by jacek
Hey,

From what I can see, the problem is that the function getPosts() does not actually return anything, instead is outputs the posts which is why you have all that stuff before you opening <html> tag. This means that the variable $post2 has no value, so when you try to use it you get nothing.

This is a pretty bad use of functions to be honest, the point of them is to basically make a block of code reusable and by having the function output very specific HTML you remove a lot of that reuseabillity. Also, the name getPosts() suggest that the function will get the posts, instead it outputs them each in a table row.

The best thing to do here would be to have the function return the data instead of outputting it, that way you could use it as you have tried to here.

Re: CMS - Editing cat works, editing post not

Posted: Wed Jul 13, 2011 11:14 am
by Linkjuice57
Hi jacek!

I've found the problem, I used $getPosts, and I should have used $getPost. Which is indeed a return.

Thanks both :-)!

Re: CMS - Editing cat works, editing post not

Posted: Wed Jul 13, 2011 1:02 pm
by Linkjuice57
Well, one more issue! :p

http://www.youtube.com/watch?v=5z0WGE7OkB0&t=9m18s

See how he has "<?php } else { header("Location: login.php"); } ?>" at the very bottom of the page? After the header has been sent out? That doesnt work of course... so I tried this:
<?php
session_start();
include ('inc/functions.php');

if ($_SESSION['user']) == FALSE) {
		header("Location: login.php");
}
else {
?>
<!DOCTYPE html>
<html>
<head>
<title>CMS</title>
</head>

<body>
<?php include 'inc/menu.php'; ?>

<table cellspacing="0" cellpadding="10">
	<thead>
    	<tr>
        	<td>Post Title</td>
            <td>Author</td>
            <td>Action</td>
        </tr>
    </thead>
    <tbody>
    	<?php getPosts(); ?>
    </tbody>
</table>

</body>

</html>
<?php } ?>
I get the following error:

Parse error: syntax error, unexpected T_IS_EQUAL in /home/credit/public_html/admin/posts.php on line 5

Am I doing something completely wrong here? Or did I forget a semicolon or something?

Re: CMS - Editing cat works, editing post not

Posted: Wed Jul 13, 2011 5:40 pm
by jacek
Linkjuice57 wrote:? Or did I forget a semicolon or something?
It;s along those lines,
if ($_SESSION['user']) == FALSE) {
should be
if (isset($_SESSION['user']) == FALSE) {

Re: CMS - Editing cat works, editing post not

Posted: Thu Jul 14, 2011 9:12 am
by Linkjuice57
Aha, I did not know I needed to use isset. Thanks for all the help so far :-)

Now lets see if I can contribute to the community!