Jump to content

Anyone fix this error :S not sure why its appearing :S?


soupy127

Recommended Posts

Hello there, please help if you can ive created a post form for posting news into my msql database and a news.php to view it. It is all working perfectly but for some reason an error keeps appearing. Here is the code below

 

<?php

if ($_POST['post'])

 

{

//get data

$title = $_POST['title'];

$body = $_POST['body'];

 

//Check for existance

if ($title&&$body)

{

//insert data

mysql_connect("localhost","root","") or die (mysql_error());

mysql_select_db("duffers") or die(mysql_error());

 

$date = date("y-m-d");

 

$insert = mysql_query("INSERT INTO news VALUES ('','$title','$body','$date')") or die(mysql_error());

die("Your news has been posted!!");

}

else

echo "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>

 

 

and the error im getting is

 

 

Notice: Undefined index: body in C:\wamp\www\Final\post.php on line 69

 

 

any ideas :S?

thanks for the help

Link to comment
Share on other sites

Tried your solution :)!

 

and got

Notice: Undefined index: post in C:\wamp\www\Final\post.php on line 64

 

I dont understande why my orignal code isnt working as i have declared the body part of the form 'body' and the actual code is adding it to the database, but still get the error :S kind of weird.

 

Body:<br>

    <textarea rows='6' cols='35' name='body'></textarea><p> 

 

Notice: Undefined index: body in C:\wamp\www\Final\post.php on line 69

 

 

:S thanks again for your help!!

 

 

Link to comment
Share on other sites

very strange. I just tried your code, with slight modification, and it works perfectly on my wamp server.

my code

<?php
   if ($_POST['post'])
   
   {
   print_r($_POST);
   //get data
$title = $_POST['title'];
$body = $_POST['body'];
   
   //Check for existance
   if ($title&&$body)
   {
    //insert data
    
   }
   else 
      echo "Please fill out Title and Body <p>";
   
   }

?>

<form 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>

 

can you post your whole script? The error happens on line 69, and if what you posted is your whole script, then line 69 doesn't exist. also comment where line 69 is please

 

Link to comment
Share on other sites

I'm fairly certain this is to do with checking existence of $_POST values. Not everyone will see the errors on their own server if they have notices turned off in PHP (which used to be the default).

 

For example the error "Undefined index 'post'" would most likely be from here

 

if ($_POST['post'])

 

Easy fix for this is to use the empty method:

 

if (!empty($_POST['post']))

 

And then when you access the data here

 

$title = $_POST['title'];
$body = $_POST['body'];

 

It can be fixed with a simple ternary operator

 

$title = empty($_POST['title']) ? '' : $_POST['title'];
$body =  empty($_POST['body']) ? '' : $_POST['body'];

 

And that should remove your issues. Undefined notices are a common problem with $_POST and $_GET data.

Link to comment
Share on other sites

<?php

if( isset($_POST['post'] && !empty($_POST['body']) && !empty($_POST['title']) ) {

    $body = $_POST['body'];

    $title = $_POST['title'];

    echo "The \$title is: {$title}<br /> and the \$body is: {$body}<br /> // or change this for your database insert, etc.

} else {

    echo "The Title and Body fields are both required";

}

?>

<form method='POST'>

  Title:<br>

    <input type='text' name='title' value="<?php if(isset($_POST['title']) ) { echo $_POST['title']; }?>"><p>

   

    Body:<br>

    <textarea rows='6' cols='35' name='body'><php if( isset($_POST['body']) ) { echo $_POST['body']; } ?></textarea><p>

   

    <input type='submit' name='post' value='Post this news'>

    </form>

Link to comment
Share on other sites

Yeah, I thought that's be the issue. I think versions of PHP pre 5.2 used to have notices turned off, so a lot of people coded like you did there and never knew the issues. Since they're now on by default, I've seen the problem a lot!

 

Luckily we have nice methods like empty to help out

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.