krustykobb Posted August 12, 2013 Share Posted August 12, 2013 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 https://forums.phpfreaks.com/topic/281074-news-page-error/ Share on other sites More sharing options...
Matic Posted August 12, 2013 Share Posted August 12, 2013 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 Link to comment https://forums.phpfreaks.com/topic/281074-news-page-error/#findComment-1444536 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.