soupy127 Posted April 25, 2010 Share Posted April 25, 2010 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 https://forums.phpfreaks.com/topic/199708-anyone-fix-this-error-s-not-sure-why-its-appearing-s/ Share on other sites More sharing options...
ChemicalBliss Posted April 25, 2010 Share Posted April 25, 2010 Undefined index = the array element specific doesn't exist. $_POST['body'] i believe does not exist. use print_r($_POST); to see what your getting. -cb- Link to comment https://forums.phpfreaks.com/topic/199708-anyone-fix-this-error-s-not-sure-why-its-appearing-s/#findComment-1048187 Share on other sites More sharing options...
soupy127 Posted April 25, 2010 Author Share Posted April 25, 2010 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 https://forums.phpfreaks.com/topic/199708-anyone-fix-this-error-s-not-sure-why-its-appearing-s/#findComment-1048202 Share on other sites More sharing options...
mikesta707 Posted April 25, 2010 Share Posted April 25, 2010 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 https://forums.phpfreaks.com/topic/199708-anyone-fix-this-error-s-not-sure-why-its-appearing-s/#findComment-1048230 Share on other sites More sharing options...
de.monkeyz Posted April 25, 2010 Share Posted April 25, 2010 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 https://forums.phpfreaks.com/topic/199708-anyone-fix-this-error-s-not-sure-why-its-appearing-s/#findComment-1048233 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2010 Share Posted April 25, 2010 <?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 https://forums.phpfreaks.com/topic/199708-anyone-fix-this-error-s-not-sure-why-its-appearing-s/#findComment-1048238 Share on other sites More sharing options...
soupy127 Posted April 25, 2010 Author Share Posted April 25, 2010 Thanks alot guys for all the replys and all your help !! I tried de.monkeyz solution and it is working brilliantly now ! Thanks alot again !! Link to comment https://forums.phpfreaks.com/topic/199708-anyone-fix-this-error-s-not-sure-why-its-appearing-s/#findComment-1048243 Share on other sites More sharing options...
de.monkeyz Posted April 25, 2010 Share Posted April 25, 2010 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 https://forums.phpfreaks.com/topic/199708-anyone-fix-this-error-s-not-sure-why-its-appearing-s/#findComment-1048245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.