Jump to content


Photo

Forum edit


  • Please log in to reply
2 replies to this topic

#1 Edmachine

Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 28 September 2007 - 06:16 AM

EDIT! I have solved it!! Seems that I have accidentally messed up isset($_POST['editpost'). Should be doedit.

EDIT2! It shows: Thank you post edited, while the post is as it was. Also, I'll update the code <_<



Hello!

I found a forum tutorial a while ago, and it worked. Now, I thought of adding EDIT DELETE LOCK etc. stuff to it. So I don't have to go to PHPMYADMIN and so that members also can edit their posts.

I've gotten to the part where you get a text area where is your original post (with all the tags), and two buttons (Save and cancel). Also, in the left table there is info on the author. What doesn't work - editing.

I somehow never have done editing of anything in a form correctly, if you know what I mean...

Anyways, here's the source:
<?php
  session_start();
  ?>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
  <title>Techgeeks.oo.lv - from websites to graphics, to music</title>
  <link href="style.css" rel="stylesheet" type="text/css">
  <style type="text/css">
  a:hover { background-color: #666; }
  a { text-decoration:none }
  </style>
  
  <style type="text/css">
   :link { color: #000 }
   :visited { color: #000 }
   :active { color: #000 }
  </style>
  </head>
  <body>
  <?php
  include('header.php');
  include('connect.php');
  ?>
  <div id=wrapper>
  <div id=main>
  <?php
  
  //PROCESS FORM
  
  if(isset($_POST['editpost'])) {
	  $id = $_POST['id'];
	  $id2 = $_POST['postid'];
  
	  $getpost = "SELECT a.*, b.Username, b.Status, b.id FROM forum a, users b WHERE postid = '$id2' AND a.Author = b.Username";
  
	  $getpost2 = mysql_query($getpost) or die(mysql_error());
  
	  $getpost3 = mysql_fetch_array($getpost2);
	  //ERROR CHECK
	  //Length check
	  if(!strlen($_POST['doedit']) > 0) {
		  die("<font color='red'><b>Error!</b> You must have a post (i.e. you need a post longer than 1 letter)!</font>");
	  }
	  //END OF ERROR CHECK
	  $newpost = $_POST['posttoedit'];
  
	  $updatepost = "UPDATE Forum SET post = $newpost WHERE post = '$id'";
  
	  $updatepost2 = mysql_query($updatepost) or die(mysql_error());
	  if($updatepost2) {
		  echo "Thank you, post edited! <a onClick='history.back(2)'>Click here</a> to go back.";
	  } else {
		  echo "<font color='red'><b>Error!</b> There was an error while editing your post. Please go back and try again.</font>";
	  }
  }
  
  //END OF FORM PROCESS
  $username = $_SESSION['s_username'];
  
  $perm="SELECT forumEDIT, forumDEL, forumLOCK, forumUNLOCK FROM Users WHERE Username = '$username'";
  
  $perm2=mysql_query($perm) or die(mysql_error());
  
  $perm3=mysql_fetch_array($perm2);
  
  //ERROR CHECK
  
  //Log in check
  if(!isset($_SESSION['s_username'])) {
	  die("<font color='red'><b>Error!</b> You must <a href='../login.php'>log in</a> to edit a post!</font>"); //If not logged in, stop
  } 
  
  
  //Permission check
  if($perm3['forumĖDIT'] == n) {
	  die("<font color='red'><b>Error!</b> You don't have permissions to edit a post!</font>"); //If the user doesn't have the permissions, stop
  }
  
  
  //Post check
  /*if(!isset($_GET['id'])) {
  die("<font color='red'><b>Error!</b> You haven't selected a post to edit!</font>"); //If no post selected, stop
  }*/
  
  //END OF ERROR CHECK
  
  $id = $_GET['id'];
  
  $getpost = "SELECT a.*, b.Username, b.Status, b.id FROM forum a, users b WHERE postid = '$id' AND a.Author = b.Username";
  
  $getpost2 = mysql_query($getpost) or die(mysql_error());
  
  $getpost3 = mysql_fetch_array($getpost2);
  ?>
  <table class='maintable'>
  <tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr>
  <tr class="mainrow"><td valign="top">
  <?php 
  echo "<a href='../profiles.php?id=".$getpost3['id']."'>".$getpost3['Username']."</a><br>Status: ".$getpost3['Status']."";
  ?>
  </td><td><form name="editpost" action="?id=<?php echo $id; ?>" method="post"><?php print"<input type=\"button\" onClick=\"document.editpost.posttoedit.value+=''\" value=\"Bold\" name=\"bold\" title=\"Add Bold\" ><input type=\"button\" onClick=\"document.post.yourpost.value+=''\" value=\"Italic\" name=\"Italic\" title=\"Add Italic\" ><input type=\"button\" onClick=\"document.post.yourpost.value+=''\" value=\"Underline\" name=\"underline\" title=\"Add Underline\" ><input type=\"button\" onClick=\"document.post.yourpost.value+=''\" value=\"Strikethrough\" name=\"strikethrough\" title=\"Add Strikethrough\" ><input type=\"button\" onClick=\"document.post.yourpost.value+=''\" value=\"Image\" name=\"image\" title=\"Add an Image\" ><input type=\"button\" onClick=\"document.post.yourpost.value+='code/code'\" value=\"Code\" name=\"code\" title=\"Add a Code\" ><input type=\"button\" onClick=\"document.post.yourpost.value+='[url="http://www.domain.com"]Link name here[/url]'\" value=\"Link\" name=\"link\" title=\"Add a Link\" ><br>"; ?>
																 <textarea cols="50" rows="10" name="posttoedit"><?php echo $getpost3['post']; ?></textarea><br>
																 <input type="submit" name="doedit" value="Edit!"><input type="button" onClick="history.back(1)" value="Cancel edit!"><input type="hidden" value="<?php echo $id; ?>" name="postid"></form></td></tr>
 </table>
 </div>
 </div>
 </body>
 </html>

What is the problem?

Edited by Edmachine, 02 October 2007 - 08:20 AM.


#2 eric81

eric81

    Young Padawan

  • Members
  • Pip
  • 258 posts
  • Gender:Male

Posted 02 October 2007 - 10:08 AM

Your variables are rather confusing. You should comment in your code. It won't display the changes yet because it is querying the table and then updating it. On a refresh it should display the changes... if not there is a problem somewhere in the code and like I said your variable names are rather confusing. Let me know what problem exactly you are having and I'll try to debug it for you. :)

Edited by zealivity5, 02 October 2007 - 10:11 AM.


#3 Edmachine

Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 02 October 2007 - 10:44 AM

<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>Techgeeks.oo.lv - from websites to graphics, to music</title>
<link href="style.css" rel="stylesheet" type="text/css">
<style type="text/css">
a:hover { background-color: #666; }
a { text-decoration:none; }
textarea ( font-size:12px; )
</style>

<style type="text/css">
 :link { color: #000 }
 :visited { color: #000 }
 :active { color: #000 }
</style>
</head>
<body>
<?php
include('header.php');
include('connect.php');
?>
<div id=wrapper>
<div id=main>
<?php

//PROCESS FORM

if(isset($_POST['doedit'])) { //If user wants to edit the post ...
	$id = $_POST['id']; 
	$id2 = $_POST['postid'];

	//ERROR CHECK
	//Length check
	if(!strlen($_POST['doedit']) > 0) {
		die("<font color='red'><b>Error!</b> You must have a post (i.e. you need a post longer than 1 letter)!</font>");
	}
	//END OF ERROR CHECK
	$newpost = $_POST['posttoedit'];
	
	$idget = $_GET['id'];

	$updatepost = "UPDATE `Forum` SET `post` = '$newpost' WHERE `post` = '$idget'";

	$updatepost2 = mysql_query($updatepost) or die(mysql_error());
	if($updatepost2) {
		echo "Thank you, post edited! <a href='index.php'>Click here</a> to return to the forum.";
	} else {
		echo "<font color='red'><b>Error!</b> There was an error while editing your post. Please go back and try again.</font>";
	}
}

//END OF FORM PROCESS
$username = $_SESSION['s_username'];

$perm="SELECT forumEDIT, forumDEL, forumLOCK, forumUNLOCK FROM Users WHERE Username = '$username'"; //Permission checking query

$perm2=mysql_query($perm) or die(mysql_error());

$perm3=mysql_fetch_array($perm2);

//ERROR CHECK

//Log in check
if(!isset($_SESSION['s_username'])) {
	die("<font color='red'><b>Error!</b> You must <a href='../login.php'>log in</a> to edit a post!</font>"); //If not logged in, stop
} 


//Permission check
if($perm3['forumĖDIT'] == n) {
	die("<font color='red'><b>Error!</b> You don't have permissions to edit a post!</font>"); //If the user doesn't have the permissions, stop
}


//Post check (going to be used when this works)
/*if(!isset($_GET['id'])) {
die("<font color='red'><b>Error!</b> You haven't selected a post to edit!</font>"); //If no post selected, stop
}*/

//END OF ERROR CHECK

$id = $_GET['id'];

$getpost = "SELECT a.*, b.Username, b.Status, b.id FROM forum a, users b WHERE postid = '$id' AND a.Author = b.Username"; //Query for selecting the post for editing

$getpost2 = mysql_query($getpost) or die(mysql_error());

$getpost3 = mysql_fetch_array($getpost2);
?>
<table class='maintable'>
<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr>
<tr class="mainrow"><td valign="top">
<?php 
echo "<a href='../profiles.php?id=".$getpost3['id']."'>".$getpost3['Username']."</a><br>Status: ".$getpost3['Status']."";
?>
</td><td><form name="editpost" action="?id=<?php echo $id; ?>" method="post"><?php print"<input type=\"button\" onClick=\"document.editpost.posttoedit.value+=''\" value=\"Bold\" name=\"bold\" title=\"Add Bold\" ><input type=\"button\" onClick=\"document.post.yourpost.value+=''\" value=\"Italic\" name=\"Italic\" title=\"Add Italic\" ><input type=\"button\" onClick=\"document.post.yourpost.value+=''\" value=\"Underline\" name=\"underline\" title=\"Add Underline\" ><input type=\"button\" onClick=\"document.post.yourpost.value+=''\" value=\"Strikethrough\" name=\"strikethrough\" title=\"Add Strikethrough\" ><input type=\"button\" onClick=\"document.post.yourpost.value+=''\" value=\"Image\" name=\"image\" title=\"Add an Image\" ><input type=\"button\" onClick=\"document.post.yourpost.value+='\[code\]\[/code\]'\" value=\"Code\" name=\"code\" title=\"Add a Code\" ><input type=\"button\" onClick=\"document.post.yourpost.value+='[url="http://www.domain.com"]Link name here[/url]'\" value=\"Link\" name=\"link\" title=\"Add a Link\" ><br>"; ?>
																<textarea cols="50" rows="10" name="posttoedit" wrap="soft"><?php echo $getpost3['post']; ?></textarea><br>
																<input type="submit" name="doedit" value="Edit!"><input type="button" onClick="history.back(1)" value="Cancel edit!"><input type="hidden" value="<?php echo $id; ?>" name="postid"></form></td></tr>
</table>
</div>
</div>
</body>
</html>


A bit changed :)

Also, it doesn't display the changes even if I visit the topic of post.

Edited by Edmachine, 02 October 2007 - 11:51 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users