form help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Doctor-D
Forum Newbie
Posts: 1
Joined: Fri May 27, 2005 8:33 am

form help

Post by Doctor-D »

Hello, I've created a small php/mysql based form to edit a news item on my homepage.

When I edit the news currently, the text box is empty. I would like to see the existing news item shown in the text box to make editing a news item easier.


Any help would be greatly appreciated :)


MYSQL info

Code: Select all

CREATE TABLE `news` (
  `newsid` int(11) NOT NULL default '0',
  `dtime` datetime default NULL,
  `title` varchar(255) NULL,
  `text` text,
  PRIMARY KEY  (`newsid`)
);
PHP code

Code: Select all

<?php
require("../head.php");
require("../common/dbconn.php");
if($submit)

  {
  $title = mysql_real_escape_string($_POST['title']);

      $text1 = mysql_real_escape_string($_POST['text']);

         $result = mysql_query("UPDATE news SET title='$title', text='$text' WHERE newsid='$newsid' ",$connect);

          echo "<b>News UPDATED Successfully!<br>You'll be redirected to Home Page after (4) Seconds";

          echo "<meta http-equiv=Refresh content=4;url=index.php>";

}

elseif($newsid)

{

        $result = mysql_query("SELECT * FROM news WHERE newsid='$newsid' ",$connect);

        while($myrow = mysql_fetch_assoc($result))

             {

                $title = $myrow["title"];

                $text1 = $myrow["text"];


?>

<br>

<h3 class="n">Edit Frontpage News</h3>

<form method="post" action="<?php echo $PHP_SELF ?>">

<input type="hidden" name="newsid" value="<? echo $myrow['newsid']?>">

<span class="n">Title</span>: 
<input name="title" class="form" value="<? echo $title; ?>" size="40" maxlength="255">

<br>

<span class="n">Text</span>: 
<textarea name="text" cols="150"  rows="10" class="form"><? echo $text; ?>
</textarea>

<br>
<br>

<input name="submit" type="submit" class="form" value="Update News">

</form>

<span class="n">
<?

              }//end of while loop

 

  }//end else
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

At the beginning of your while loop, you set

Code: Select all

$text1 = myrow["text"]
and then echo

Code: Select all

<? echo $text; ?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply