Ell20 Posted October 23, 2007 Share Posted October 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
only one Posted October 23, 2007 Share Posted October 23, 2007 Update it where the message id is, not the club id. $update = "UPDATE `news` SET `news` = '$message' WHERE `message_id` = '$message_id'"; Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 23, 2007 Author Share Posted October 23, 2007 Ok thanks, How would I go about adding a "Edit News" link to the bottom of each news item so that the news item is put into the textarea rather than just the first news item? Cheers Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 23, 2007 Share Posted October 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 23, 2007 Author Share Posted October 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 23, 2007 Share Posted October 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 23, 2007 Author Share Posted October 23, 2007 <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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 23, 2007 Share Posted October 23, 2007 don't put that inside <? ?> tags. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 23, 2007 Author Share Posted October 23, 2007 Thank you, working now. However im still learning php so I dont understand how to get the message into the textarea Cheers Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 23, 2007 Author Share Posted October 23, 2007 Bump Can anyone help finish this off? I need to get the news into a textarea which I can then use sql to UPDATE the database once the news item has been edited? Cheers Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 23, 2007 Author Share Posted October 23, 2007 Anyone? Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 24, 2007 Share Posted October 24, 2007 use post data $_POST['message'] $update = "UPDATE news SET news='{$_POST['message'] }' WHERE club_id='$club_id'" or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 24, 2007 Author Share Posted October 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 24, 2007 Share Posted October 24, 2007 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 ? Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 24, 2007 Author Share Posted October 24, 2007 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? Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 24, 2007 Share Posted October 24, 2007 ic hmm how do you want to edit your news one by one just like how you edit your post in this forum or you want edit your message all together like 10 text boxes will appear and edit your record all at the same time? Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 24, 2007 Author Share Posted October 24, 2007 This is my layout, once the Edit News button is clicked id like the message to appear in a textarea then you can submit any changes. See code for Edit News button on previous page. Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 24, 2007 Share Posted October 24, 2007 note: not tested <? 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 24, 2007 Author Share Posted October 24, 2007 Thanks for the code, no errors but the code dosent appear to do anything?!?! Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 24, 2007 Share Posted October 24, 2007 you have to edit my code and as you can see it will only works once the link for edit news is press and i use id as my query string i dont know if thats what you also use Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 24, 2007 Author Share Posted October 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 24, 2007 Share Posted October 24, 2007 <?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>'; } ?> Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 24, 2007 Author Share Posted October 24, 2007 Thanks, I think were getting somewhere, when I click edit now it brings up the textarea but it is currently empty? Cheers Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 24, 2007 Author Share Posted October 24, 2007 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 24, 2007 Share Posted October 24, 2007 look at your condition in your update WHERE club_id='$club_id'" is that right? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.