Jump to content

News Page Error


krustykobb

Recommended Posts

Hi,
 
I am writing some code for a news page for a website I am developing, and I get the following error:
 

 

Notice: Undefined index: body in C:\xampp\htdocs\post.php on line 12
 
The code I am working with is:
 
<html> <h1>Post News</h1><hr> <?php if (isset($_POST['post'])){//get data$title = $_POST['title'];$body = $_POST['body']; //check for existance if ($title&&$body){//insert data mysql_connect ("localhost","root","test1234","") or die (mysql_error ());mysql_select_db("beavers") or die (mysql_error());  $date = date ("d-m-Y");$insert = mysql_query("insert INTO news VALUES '','$title','$body','$date')") or die (mysql_error()) ; die("Your news has been posted");}elseecho "Please fill out title and body<p>";}       ?> <form action='post.php' method='POST'>Title:<br>        <input type='text' name='title'><p> Body:<br>        <textarea rows='6' cols='35 name='body'></textarea><p>                <input type='submit' name='post' value='Post this news'/> </form>   <hr></html>

 
And the actual news page is:
 
<html><h1>News</h1><hr> <?php //connect mysql_connect ("localhost","root","test1234","") or die (mysql_error ());mysql_select_db("beavers") or die (mysql_error());   //query database $getnews = mysql_query("SELECT * FROM news") or die (mysql_query); while ($row = mysql_fetch_assoc ($getnews)){ //get data$id = $row['id'];$title = $row['title'];$body = $row['body'];$date = $row['date'];   echo "<b>$title posted on $date</b><br>"; echo nl2br ($body); echo"<hr>"; } ?>      </html>

 
Any help is greatly appreciated.
Link to comment
Share on other sites

You are including $body and all the other variables before they are stated... In php code runs from top to bottom. Solution would be to move your if statements:

if ($title&&$body)

below the code for your news page

Edited by Matic
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.