Undefined Index Error |
Undefined Index Error |
krustykobb |
Aug 12 2013, 08:30 AM
Post
#1
|
Newbie Group: Members Posts: 13 Joined: 14-July 13 Member No.: 19,421 |
As I am trying to code a page for a website I am developing I get the following error:
QUOTE Notice: Undefined index: post in C:\xampp\htdocs\post.php on line 8 The code for the error is: CODE if ($_POST['post']) { //get data $title = $_POST['title']; $body = $_POST['body']; //check for existance if ($title&&$body) { echo "insert"; } else echo "Please fill out title and body<p>"; } The code for my entire file is: ( also can't seem to get the text to say insert when information is inputted into the form) CODE <html> <h1>Post News</h1> <hr> <?php if ($_POST['post']) { //get data $title = $_POST['title']; $body = $_POST['body']; //check for existance if ($title&&$body) { echo "insert"; } 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> <hr> </html> Any help is greatly appreciated This post has been edited by krustykobb: Aug 12 2013, 08:32 AM |
Christian J |
Aug 12 2013, 11:05 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
QUOTE Notice: Undefined index: post in C:\xampp\htdocs\post.php on line 8 That's not an actual error (just a notice), it just means $_POST['post'] is undefined. This happens when the page is loaded without first submitting the form. To get rid of the notice you might use isset(): CODE if(isset($_POST['post'])) Sidenotes: CODE //get data $title = $_POST['title']; $body = $_POST['body']; Don't forget to sanitize the form data before using it for anything. CODE if ($title&&$body) Don't know if it's good practice to omit spaces between the variables and the operator like that? QUOTE also can't seem to get the text to say insert when information is inputted into the form Seems the missing singlequote in the COLS attribute value makes the field fail to be recognized: CODE <textarea rows='6' cols='35 name='body'> |
krustykobb |
Aug 12 2013, 11:16 AM
Post
#3
|
Newbie Group: Members Posts: 13 Joined: 14-July 13 Member No.: 19,421 |
Thanks, so many noob mistakes ... I have fixed the ones you have pointed out and when trying to add a post I now get the following error:
QUOTE You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''','test','test ','12-08-2013')' at line 1 |
Christian J |
Aug 12 2013, 02:07 PM
Post
#4
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
That's an SQL error, but there's no SQL in the code example you posted before...
|
krustykobb |
Aug 12 2013, 02:55 PM
Post
#5
|
Newbie Group: Members Posts: 13 Joined: 14-July 13 Member No.: 19,421 |
How would I fix this error? I believe I have set up my database correctly.
|
Christian J |
Aug 12 2013, 03:05 PM
Post
#6
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
Impossible to say without seeing the SQL (and PHP) code.
|
krustykobb |
Aug 12 2013, 03:21 PM
Post
#7
|
Newbie Group: Members Posts: 13 Joined: 14-July 13 Member No.: 19,421 |
I believe that this is my SQL code:
QUOTE -- phpMyAdmin SQL Dump -- version 4.0.4 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Aug 12, 2013 at 10:20 PM -- Server version: 5.5.32 -- PHP Version: 5.4.16 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `beavers` -- -- -------------------------------------------------------- -- -- Table structure for table `news` -- CREATE TABLE IF NOT EXISTS `news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(100) NOT NULL, `body` text NOT NULL, `date` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Dumping data for table `news` -- INSERT INTO `news` (`id`, `title`, `body`, `date`) VALUES (1, 'Test News', 'Hello!\r\nThis is a new line...\r\n\r\nThis is a new paragraph...', '2013-08-12'), (2, 'Another Test', 'This is a line\r\n\r\nThis is a new paragraph', '2013-08-11'); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; And this is the PHP code that returns the error when I try and use it: QUOTE <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"); } 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> <hr> </html> |
Christian J |
Aug 12 2013, 06:09 PM
Post
#8
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
I believe that this is my SQL code: I was actually thinking of the following: CODE $insert = mysql_query("insert INTO news VALUES '','$title','$body','$date')") or die (mysql_error()); --I think there should be a opening parenthese after VALUES. BTW (I haven't done SQL in a long while), should the ID column really be empty like that when it's supposed to auto-increment? Not sure how to do it when the INSERT INTO doesn't specify columns explicitly. |
krustykobb |
Aug 13 2013, 02:48 AM
Post
#9
|
Newbie Group: Members Posts: 13 Joined: 14-July 13 Member No.: 19,421 |
Thanks - I have finally managed to get it working
However, when the news appears on the news.php page the date is shown as the following: A piece of test news posted on 0000-00-00 Any idea why this is? |
krustykobb |
Aug 13 2013, 12:03 PM
Post
#10
|
Newbie Group: Members Posts: 13 Joined: 14-July 13 Member No.: 19,421 |
edit: all fixed
|
Lo-Fi Version | Time is now: 24th April 2024 - 11:12 AM |