PHP - Undefined Index Error

Status
Not open for further replies.

AlexMK92

Seniorius Lurkius
1
Hi guys hopefully I've came to the correct part of the forum, correct me if I'm wrong though ;)
Im pretty new to coding manually so excuse my sloppy scripts, I've just switched from using a WYSIWYG

Finally moved on to the last part of my system , the actual posting of threads to my database, and I could sware that these index errors have it in for me ;) Heres my code and I shall justify where the Index Definitions are now and maybe someone can see my flaw!

create-posts.php (the code which contains the divs)

Code:
   <!--////////////////////////////////////////////////////////
    // Project : Absolute Beginners log in system ///////////
    // Purpose : A2 Level Coursework for ICT ////////////////
    // Teacher : Mr L Hawkins ///////////////////////////////
    // Student : Alex Sims //////////////////////////////////
    // Candidate No. : 4173 /////////////////////////////////
    // Centre No : 27168 ////////////////////////////////////
    /////////////////////////////////////////////////////////-->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"/></script>
    <script type="text/javascript" src="javascript/create-cat.js"/></script>
    <script type="text/javascript" src="javascript/custom.js"/></script>
    <?php include_once('includes/header.php') ; ?>
    <?php include_once('scripts/create_posts.php') ; ?>
    <?php include_once('scripts/session_a.php') ; ?>
    <?php include_once ('includes/posts-header.php')?>
    <script type="text/javascript" >
       $(document).ready(function() {
          $("#markItUp").markItUp(mySettings);
       });
    </script>
               
                <div class="col-full">
                    <ul id="submenu">
                       
                    </ul>
                    </ul>
                   
                    </ul>
                </div> <!-- end subheader -->
                <!-- Start Main Div section -->
                <div class="col-full">
                <div id="leftsection">
                    <h2>Create A Post Here</h2>
                   
                    <form method="post" action"create_posts.php"> <!-- FORM START, END LINE 94 -->
                                   
                    <div class="form-elements">
                        <input type"text" name"title" placeholder="Add Title Here" id="title"/>
                       
                </div><!-- End of Left Section -->
               
               
                    <div class="form-elements">
                        <textarea id="markItUp" name"content"></textarea>
                    </div> <!-- Installed Text Editor -->
                   
                <!-- submit button-->       
                <div class="form-button">
               <input type="submit" name="submit" value="Submit" id="submit"/>
                </div>
                <!-- end submit button-->
                   
                    <div class="form-elements">
                        <input type"text" name"metatitle" placeholder="Meta Title" id="metatitle"/>
                </div>
               
                <div class="form-elements">
                        <input type"text" name"metadescription" placeholder="Meta Description" id="metadescription"/>
                </div>
               
                <div class="form-elements">
                        <input type"text" name"metarobots" placeholder="Meta Robots" id="metarobots"/>
                </div>
               
                <div class="form-elements">
                        <input type"text" name"metatags" placeholder="Meta Tags" id="metatags"/>
                </div>
               
               
               
                <div id="rightsection">
                    <h2>Categories</h2>
                   
                    <div id="listcategores">
    <!-- INCLUDE SCRIPT TO KEEP CATEGORIES VISIBLE ON PAGE REFRESH -->
                    <?php include_once('scripts/get-categorys.php'); ?>
                   
                   
    <!--THIS BLOCK HAS BEEN QUOTED OUT TO BE USED IN CREATE-CAT.PHP AS WE NO LONGER NEED IT TO DISPLAY-->
                    <!--    <div class="form-elements2">
                            <input type="checkbox" name="categoryname" value="a" id="categoryname"/> Category 1
                </div>                   
                        <div class="form-elements2">
                            <input type="checkbox" name="categoryname" value="" id="categoryname"/> Category 2
                </div>  end category list -->
    <!--\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ END OF BLOCK QUOTE OUT //////////////////////////-->
               
                <div id="createcat">
        <div class="form-elements">
               <input type="text" name="categorycreate" id="categorycreate" placeholder="Add New Category"/>
                </div><!-- end createcat-->
               
                    </form> <!-- END OF FORM -->
                   
               
                <div class="clear"></div>
               
               
        </div><!--end mainWrapper-->
    </body>
    </html>

create_posts.php (where the variables are defined)
Code:
    <?php
           
            include_once('connection.php');
            include_once('session_a.php');
           
           
            //IF SUBMIT BUTTON IS PRESSED THEN EXECUTE SCRIPT
            if(isset($_POST['submit']))
            // VARIABLES ASSIGNED TO ALL FIELDS WHICH WILL BE USED TO POST
            {
                $title = $_POST['title'];
                $content = $_POST['content'];
                $metatitle = $_POST['metatitle'];
                $metadescription = $_POST['metadescription'];
                $metarobots = $_POST['metarobots'];
                $metatags = $_POST['metatags'];
                $categoryname = $_POST['categorycreate'];
               
                // IMPLODE TURNS ARRAY INTO A STRING
                if(empty($title) || empty($content))
                {
                    echo 'A Title and some content is required to create a post.';
                }
                elseif(empty($categoryname))
                {
                    $category = 'Uncategorized';
                }
                    else
                    {
                        $category = implode(',',$categoryname);
                       
                        $sql = mysql_query("INSERT INTO posts (title,description,category,post_meta,post_robots,meta_title,meta_description,created_by,created_on)
                        VALUES ('$title','$content','$category','$metatags','$metarobots','$metatitle','$metadescription','$session_name',now())");
                       
                        $affected_rows = mysql_affected_rows();
                       
                        if($affected_rows == 1)
                        {
                            $id = mysql_insert_id();
                            header("location: create-posts.php?postid=$id.");
                            echo 'Post Created succesfully';
                        }
                    }
               
            }
           
    ?>

I get returned with undefined index errors from lines 11 through 16 but 17 is defined. I defined them via the appropriate DIV name and attained this info through firebug, all the meta fields are defined on the first linked sheet and the other variables are defined in the installed text editors parameter. I checked the case sensitivity but that still returned no positive outcome. I really need to get better at script debugging!

If anyone has any suggestions will be good to here

Thanks
Alex.
 

Schluppy

Ars Tribunus Militum
2,486
I guess I'll take one for the team...

First and foremost, do NOT put this code on a publicly accessible server, EVER. I understand that you're just learning but you've made a pretty huge mistake here. Your code is vulnerable to SQL Injection. Google it and learn how to prevent it before you continue.

Second, your form elements are messed up: you're missing the = in almost every name attribute. This is likely what's causing the error: PHP is looking for an index that doesn't exist because you haven't defined it properly.

While you're learning, get used to running your HTML through a validator. It'll help you find mistakes like this.
 
Status
Not open for further replies.