CMS - Editing cat works, editing post not
Posted: Wed Jul 13, 2011 9:49 am
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:
And this is the actual PHP code: (edit.php)
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?