Jump to content

[SOLVED] Edit Message In PHP


Ell20

Recommended Posts

Hey,

 

I have a database of news items.

On the news page all news items related to the particular club that the user is logged into is displayed.

 

I want the admin of the club to be able to edit each particular message by clicking a link "Edit News" which will be displayed under each news item if you are an admin.

 

Once "Edit News" has been selected I would like the message to be placed into the textarea for editing. Then once submitted the new item is updated.

 

I have done a similar thing without the edit button (see code) however for this example it updates all the news items with the same information.

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0" align="center" cellspacing="0" border="0" class="game">
<tr>
<th>
Edit News:
</th>
</tr>
<tr>
<td align="center">
<?php
echo "<textarea cols=\"91\" rows=\"10\" name=\"message\">{$message}</textarea>"; ?>
</td>
</tr>
<tr>
<td align="center">
<?php
echo "<input name=\"submit\" type=\"submit\" />"; ?>
</td>
</tr>
<?php
echo "</form>";
if (isset($_POST['submit'])) {
   $message = $_POST['message'];
   $update = "UPDATE news SET news='$message' WHERE club_id='$club_id'" or die(mysql_error());
   mysql_query($update) or die(mysql_error());
   echo '<center><h3>Message Updated!</h3></center>';
}}}}} ?>

 

Thanks for any help

include the id of the message in the link, then GET it on the linked page

 

<a href='editmessage.php?messageid=$msg_id'>Edit Message</A>

 

then on editmessage.php,

 

$message_id = $_GET['messageid'];

$sql = "SELECT * FROM `news` WHERE `message_id` = '$message_id'";

// etc.

Thanks, ive got the Edit Message link in place now and its correctly using the new_id value.

 

The way you have written the next bit, requires me to created a new page: editmessage.php, is there anyway in which I can direct the message into a textarea on the same page rather than on a different page?

 

Cheers

sure, just direct the page to itself with an extra bit in the URL to tell the page that the user wants to edit the page, maybe

 

<a href='<?=$_SERVER['PHP_SELF']."?messageid=$msg_id";?>&mode=edit'>Edit Message</A>

 

The look for $_GET['edit'] to determine whether to load the text into a textarea to edit or, instead just display it.

<a href='<?=$_SERVER['PHP_SELF']."?news_id=$newsid";?>&mode=edit'>Edit Message</a>

 

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\program files\easyphp1-8\www\html\news.php on line 49

 

Cheers

I understand how to update the database but I am having trouble getting the value into the textarea in the first place?

 

$query = "SELECT * FROM news WHERE club_id = '$club_id'";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
$newsid = $row['news_id'];

    echo "<b>Title: </b>" ;
echo $row['title'];
echo "<br>";
echo $row['news'];
echo "<br>";
?>
<a href='<?=$_SERVER['PHP_SELF']."?news_id=$newsid";?>&mode=edit'>Edit News</a>
<hr>
<?php } ?>
</td>
</tr>
</table>

 

Cheers

i dont understand to get the value of the text area you only need to do something like this

echo $_POST[message];

 

or maybe your update query is below your select query thats why you will see changes on the second run or when the page is refreshed ?

Im not sure were thinking along the same lines?

 

I already have a form where I can create new news items by typing in the title and news.

 

However I want the admin to be able to  edit each of the news items so that they can be changed once posted.

 

I have already done something like this for a welcome message but as it was only the 1 message it was easy however my news page can have hundreds of news items.

 

When the user presses the edit button I would like the message which is currently stored to be printed in the text area, the user can then edit the news and press submit, which updates the news item?

note: not tested  ;D

<? 
if(isset($_GET['id'])){
$query = "SELECT * FROM news WHERE club_id = '{$_GET['id']}'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result)
$newsid = $row['news_id'];
$message = $row['message'];//added by teng
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<textarea name="my_messages" cols="" rows=""><?=$message?></textarea>
<input name="submit" type="submit" /> 
</form>
<? }
if (isset($_POST['submit'])) {
   $message = $_POST['message'];
   $update = "UPDATE news SET news='{$_POST['my_messages']}' WHERE club_id='$club_id'" or die(mysql_error());
   mysql_query($update) or die(mysql_error());
   echo '<center><h3>Message Updated!</h3></center>';
}
?>

Im confused, here is my code, not sure which variables are right anymore, but I no 'id' isnt right:

 

<?php

$club_id = mysql_query("SELECT club_id FROM users WHERE user_id = '{$_SESSION['user_id']}'")
    OR DIE(mysql_error());
    $row = mysql_fetch_assoc($club_id);
    $club_id = $row['club_id'];
    
    $clubname = mysql_query("SELECT clubn FROM club WHERE club_id = '$club_id'")
        OR DIE(mysql_error());
    $row = mysql_fetch_assoc($clubname);
    $clubname = $row['clubn'];
    
    $message = mysql_query("SELECT * FROM news WHERE club_id = '$club_id'")
        OR DIE(mysql_error());
    $row = mysql_fetch_assoc($message);
    $news = $row['news'];

$query = "SELECT * FROM news WHERE club_id = '$club_id'";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
$newsid = $row['news_id'];

    echo "<b>Title: </b>" ;
echo $row['title'];
echo "<br>";
echo $row['news'];
echo "<br>";
?>
<a href='<?=$_SERVER['PHP_SELF']."?news_id=$newsid";?>&mode=edit'>Edit News</a>
<hr>
<?php } ?>
</td>
</tr>
</table>


<?php  
if(isset($_GET['id'])){
$query = "SELECT * FROM news WHERE club_id = '{$_GET['id']}'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$newsid = $row['news_id'];
$message = $row['message'];//added by teng
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<textarea name="my_messages" cols="100" rows="10"><?=$message?></textarea>
<input name="submit" type="submit" /> 
<?php }
if (isset($_POST['submit'])) {
   $message = $_POST['message'];
   $update = "UPDATE news SET news='{$_POST['my_messages']}' WHERE club_id='$club_id'" or die(mysql_error());
   mysql_query($update) or die(mysql_error());
   echo '<center><h3>Message Updated!</h3></center>';
}
?>

 

Appreciate any help

<?php

$club_id = mysql_query("SELECT club_id FROM users WHERE user_id = '{$_SESSION['user_id']}'")
    OR DIE(mysql_error());
    $row = mysql_fetch_assoc($club_id);
    $club_id = $row['club_id'];
    
    $clubname = mysql_query("SELECT clubn FROM club WHERE club_id = '$club_id'")
        OR DIE(mysql_error());
    $row = mysql_fetch_assoc($clubname);
    $clubname = $row['clubn'];
    
    $message = mysql_query("SELECT * FROM news WHERE club_id = '$club_id'")
        OR DIE(mysql_error());
    $row = mysql_fetch_assoc($message);
    $news = $row['news'];

$query = "SELECT * FROM news WHERE club_id = '$club_id'";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
$newsid = $row['news_id'];

    echo "<b>Title: </b>" ;
echo $row['title'];
echo "<br>";
echo $row['news'];
echo "<br>";
?>
<a href='<?=$_SERVER['PHP_SELF']."?news_id=$newsid";?>&mode=edit'>Edit News</a>
<hr>
<?php } ?>
</td>
</tr>
</table>


<?php  
if(isset($_GET['news_id'])){
$query = "SELECT * FROM news WHERE club_id = '{$_GET['news_id']}'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$newsid = $row['news_id'];
$message = $row['message'];//added by teng
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<textarea name="my_messages" cols="100" rows="10"><?=$message?></textarea>
<input name="submit" type="submit" /> 
<?php }
if (isset($_POST['submit'])) {
   $message = $_POST['message'];
   $update = "UPDATE news SET news='{$_POST['my_messages']}' WHERE club_id='$club_id'" or die(mysql_error());
   mysql_query($update) or die(mysql_error());
   echo '<center><h3>Message Updated!</h3></center>';
}
?>

Ok I have it working to a certain level however when I submit query it is updating all the news items with the same text I just typed in:

 

<?php

$club_id = mysql_query("SELECT club_id FROM users WHERE user_id = '{$_SESSION['user_id']}'")
    OR DIE(mysql_error());
    $row = mysql_fetch_assoc($club_id);
    $club_id = $row['club_id'];
    
    $clubname = mysql_query("SELECT clubn FROM club WHERE club_id = '$club_id'")
        OR DIE(mysql_error());
    $row = mysql_fetch_assoc($clubname);
    $clubname = $row['clubn'];
    
    $message = mysql_query("SELECT * FROM news WHERE club_id = '$club_id'")
        OR DIE(mysql_error());
    $row = mysql_fetch_assoc($message);
    $news = $row['news'];

$query = "SELECT * FROM news WHERE club_id = '$club_id'";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
$newsid = $row['news_id'];

    echo "<b>Title: </b>" ;
echo $row['title'];
echo "<br>";
echo $row['news'];
echo "<br>";
?>
<a href='<?=$_SERVER['PHP_SELF']."?news_id=$newsid";?>&mode=edit'>Edit News</a>
<hr>
<?php } ?>
</td>
</tr>
</table>


<?php  
if(isset($_GET['news_id'])){
$query = "SELECT * FROM news WHERE news_id = '{$_GET['news_id']}'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$newsid = $row['news_id'];
$message = $row['news'];//added by teng
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<textarea name="my_messages" cols="100" rows="10"><?=$message?></textarea>
<input name="submit3" type="submit" /> 
<?php }
if (isset($_POST['submit3'])) {
   $message = $_POST['news'];
   $update = "UPDATE news SET news='{$_POST['my_messages']}' WHERE club_id='$club_id'" or die(mysql_error());
   mysql_query($update) or die(mysql_error());
   echo '<center><h3>Message Updated!</h3></center>';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.