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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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>';
}
?>

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.