The question might seem a little odd but I will show you what I mean :)
So I have made a webpage that contains comments that have been made by authorised users (teachers). That is another piece of code in another page so that works perfectly. Of course it should be possible for the person who made the comment to change his own comments in case of mistakes etc.
I managed to show the user his own comments with his comments in seperate text areas. The text areas have no specific name and are just called txtComment, how do I make them unique for every comment (which is flexible because comments can be added later). I think that is the issue why I cannot update the changed/adapted textareas after I press the button.
I hope you guys understand what I am trying to say :p
I changed Dutch words mostly to English so if there is a spelling error that is due to that :)
This is the code:
<?php
$sqlKlas = "Select * from tblcomments WHERE ForWho='".$_SESSION['User']."' AND ForWho='6HA1' OR VoorWie='6HA2'";
$resultKlas = mysql_query($sqlKlas) or die(mysql_error());
if (isset($_POST['btnAdaptCommentKlas'])){
while($rowKlas = mysql_fetch_array($resultKlas)){
$CommentNr = $rowKlas['CommentNr'];
$sqlUpdate = "Update tblcomments SET Comment='".$_POST['txtComment']."' WHERE CommentNr=$CommentNr";
$resultUpdate = mysql_query($sqlUpdate) or die(mysql_error());
echo "<tr>";
echo "<td>";
?><textarea name="txtComment" cols="80" rows="5"><?php echo $rowKlas['Comment']; ?></textarea><?php
echo "</td>";
echo "<td>".$rowKlas['ForWho']."</td>";
echo "<td>".$rowKlas['Datum']."</td>";
echo "</tr>";
}
}
else {
while($rowKlas = mysql_fetch_array($resultKlas)){
echo "<tr>";
echo "<td>";
?><textarea name="txtComment" cols="80" rows="5"><?php echo $rowKlas['Comment']; ?></textarea><?php
echo "</td>";
echo "<td>".$rowKlas['ForWho']."</td>";
echo "<td>".$rowKlas['Datum']."</td>";
echo "</tr>";
}
}
I would do it a little differently - updating separate to listing out the comments. Also You would have to set the textarea name containing the comment ID, like in my example:
<?php
$sqlKlas = "Select * from tblcomments WHERE ForWho='".$_SESSION['User']."' AND ForWho='6HA1' OR VoorWie='6HA2'";
$resultKlas = mysql_query($sqlKlas) or die(mysql_error());
if (isset($_POST['btnAdaptCommentKlas'])){
foreach($_POST['txtComment'] as $key => $value) {
$sqlUpdate = "UPDATE tblcomments SET Comment='".mysql_real_escape_string($value)."' WHERE CommentNr=".(int)$key;
$resultUpdate = mysql_query($sqlUpdate) or die(mysql_error());
}
}
while($rowKlas = mysql_fetch_array($resultKlas)){
$CommentNr = $rowKlas['CommentNr'];
echo "<tr>";
echo "<td>";
?><textarea name="txtComment[<?php echo $CommentNr; ?>]" cols="80" rows="5"><?php echo $rowKlas['Comment']; ?></textarea><?php
echo "</td>";
echo "<td>".$rowKlas['ForWho']."</td>";
echo "<td>".$rowKlas['Datum']."</td>";
echo "</tr>";
}
Also I would recommend not to use mysql_*
function but use PDO
or at least mysqli_*
instead. Using mysql_real_escape_string()
to escape the user input is highly recommended.
Also using MVC
and separating the data manipulation (updating, fetching, creation) and data presentation (in an HTML template) would be much nicer and more maintainable for further development.
Instead of txtComment
use txtComment[unique_id]
. This way you can do something like
foreach(array_keys($_GET['txtComment']) as $id => $comment) { ...
then try this
$sqlUpdate = "Update tblcomments SET Comment='".$_POST['txtComment']."' WHERE OpmerkingNr='".$OpmerkingNr."' ";
and change this
$sqlKlas = "Select * from tblcomments WHERE ForWho='".$_SESSION['User']."' AND ForWho='6HA1' OR VoorWie='6HA2'";
to this
$sqlKlas = "Select * from tblcomments WHERE ForWho='".$_SESSION['User']."' AND (ForWho='6HA1' OR VoorWie='6HA2' )";
you could also check the user if logged in or not here
if (isset($_POST['btnAdaptCommentKlas'])){
try change it to
if (isset($_POST['btnAdaptCommentKlas']) AND $_SESSION['User'] !=0 ){
obs: I advice you to use PDO instead.