so I'm trying to use only php and html to build a simple comment system. I can Login users and post comments but editing comments does not work.
So i save posted comments in a txt file with the format:
User|commentSignature|commentContent
Where comment signature is a substring of commentContent. Then when the user is logged in i show his comments in a forms textare so he can submit that form and edit them easily.
Here is the form to edit comments:
$line = trim($line);
$commentString= 'Comment';
$arr = explode("|", $line, 3);
// This doesent work
// It is about editing comments
if (isset($_SESSION['user']) and $_SESSION['user'] == $arr[0]) { ?>
<form action="editComment.php" method="POST">
<textarea rows="10" cols="30" name="commentContent"> <?php echo "Here is youre old comment: " . $arr[2] ?></textarea>
<br />
<input type="hidden" name="oldCommentSignature" value= "<?php echo $arr[1]; ?>">
<input type="submit" value="Edit"> <br />
</form>
So as you can see i send a hidden value oldCommentSignature to be able to find the comment I shall overwrite.
So far so good i print the $_POST and $_SESSION values and they get sent but my overwriting method in editComment.php is not working.
<?php session_start();
print_r($_POST); print_r($_SESSION);
$handle = fopen ( "save.txt", "r+" ); if ($handle &&
isset($_SESSION['user']) && $_POST) {
while ( ($line = fgets ( $handle )) !== false ) {
$line = trim($line); $arr = explode("|", $line, 3);
//$compareValue = $arryOfLine[1];
if ($_POST["oldCommentSignature"] == $arr[1]) {
$newCommentSignature = substr($_POST ["commentContent"], 0, 9);
$nameAndTimeAndContent = $_SESSION['user'] . "|" .
$newCommentSignature . "|" . $_POST ["commentContent"] . "
";
fwrite($handle, $nameAndTimeAndContent);
break;
}
}
} fclose ( $handle );
?>
Sometimes nothing is happening and sometimes i get the message
Notice: Undefined offset: 1 in /Applications/XAMPP/xamppfiles/htdocs/tr/editComment.php on line 16*