SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot themissingelf's Avatar
    Join Date
    Nov 2001
    Location
    UK
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined index errors......

    I've trawled through a few of the other posts on this subject, however, I've not been able to successfully apply the recommendations to my script. Can anyone spot the reason why I am getting "Undefined index" errors on lines 8, 21, 49, 61, and 73 (all marked in the comments) of this script:
    PHP Code:
    <html>
    <head>
    <title>Maintain Categories</title>
    <meta name="generator" content="Sausage Software HotDog Professional 6">
    </head>
    <body>
    <?php
    if ($_GET['addcat']): //this is line 8
    ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <p>Type new category here:<br />
    <input type="text" name="category" maxlength="50">
    <br />
    <input type="submit" name="entercat" value="submit" />
    </P>
    </form>

    <?php
    $cat 
    $_GET['cat'];
    $catid $_GET['amendcat'];
    elseif (
    $_GET['amendcat']): //this is line 21
    ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <p>Amend category:<br />
    <input type="text" name="category" maxlength="50" value="<?=htmlspecialchars($_GET['cat'])?>" />
    <input type="hidden" name="id" value="<?=htmlspecialchars($_GET['amendcat'])?>" />
    <br />
    <input type="submit" name="changecat" value="submit" />
    </P>
    </form>

    <ol>
    <?php
    else: //display normal page
    //connect to the database server
    $dbcnx = @mysql_connect("localhost","root","xxxxxxxx");
    if (!
    'dbcnx') {
        echo(
    "<p>Unable to connect to the ".
            
    "database server at this time.</p>");
        exit();
    }
    if (! @
    mysql_select_db("stock")){
        echo(    
    "<p>Unable to locate the stock ".
                
    "database at this time.</p>");
        exit();
    }
    //if stock item has been added
    //add it to the database.
    //this is line 49
    if ($_POST['entercat'] == "submit") {
    $sql "INSERT INTO categories SET
            Category='
    $_POST[category]'";
        if(@
    mysql_query($sql)) {
            echo(
    "<p>Your category has been added.</P>");
        }else {
            echo(
    "<p>Error adding category: ".
                    
    mysql_error()."</p>");
        }
    }
    //delete all stocklookup entries for the category
    //along with the entry for the category
    //this is line 61
    $removecat $_GET['removecat'];
    if (
    $removecat) {
    $ok1 = @mysql_query("DELETE FROM stocklookup WHERE CID=$removecat");
    $ok2 = @mysql_query("DELETE FROM categories WHERE ID=$removecat");
        if (
    $ok1 and $ok2){
        echo(
    "<p>The category has been removed.</p>");
        } else {
        echo(
    "<p>Error removing category:" mysql_error(). "</p>");
        }
    }
    //if stock item has been added
    //add it to the database.
    //this is line 73
    if ($_POST['changecat'] == "submit") {
    $sql "UPDATE categories SET
            Category='
    $_POST[category]' WHERE ID=$_POST[id]";
        if(@
    mysql_query($sql)) {
            echo(
    "<p>Your category has been amended.</P>");
        }else {
            echo(
    "<p>Error amending category: ".
                    
    mysql_error()."</p>");
        }
    }

    echo(
    "<p>Here are all the categories:"."</p>");

     
    //request the text of all the categories
    $result = @mysql_query("SELECT ID, Category FROM categories");    if(!$result) {
            echo(    
    "<p>Error performing query:" .mysql_error().                "</p>");
            exit();
    }
    //display the text of each category in a paragraph
    While ($row mysql_fetch_array($result)) {
        
    $catid $row["ID"];
        
    $category htmlspecialchars($row["Category"]);
        echo(
    "<li>$category "."[<a href='$_SERVER[PHP_SELF]?removecat=$catid'>Remove</a> |"." <a href='$_SERVER[PHP_SELF]?amendcat=$catid&cat=$category'>Amend</a>]"."</li>");
    }
    //When clicked, this link will load this page with the 
    //category submission form displayed.
            
    echo("<p><a href='$_SERVER[PHP_SELF]?addcat=1'>Add a new category</a></p>");

        endif;

    ?>
    </ol>
    <a href="admin.html">Return to main menu</a>
    </body>
    </html>

  2. #2
    SitePoint Zealot themissingelf's Avatar
    Join Date
    Nov 2001
    Location
    UK
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - fixed it! New code shown below in case anyone else stumbles across this problem. Note the inclusion of the isset function at the offending lines (the eagle eyed amongst you will have spotted some other poor coding and spurios lines that were not causing errors but have changed in the script below):


    PHP Code:
    <html>
    <head>
    <title>Maintain Categories</title>
    <meta name="generator" content="Sausage Software HotDog Professional 6">
    </head>
    <body>
    <?php
    if (isset($_GET['addcat'])): //if user wants to add a category
    ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <p>Type new category here:<br />
    <input type="text" name="category" maxlength="50">
    <br />
    <input type="submit" name="entercat" value="submit" />
    </P>
    </form>

    <?php
    elseif (isset($_GET['amendcat'])): //if user wants to amend a category
    ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <p>Amend category:<br />
    <input type="text" name="category" maxlength="50" value="<?=htmlspecialchars($_GET['cat'])?>" />
    <input type="hidden" name="id" value="<?=htmlspecialchars($_GET['amendcat'])?>" />
    <br />
    <input type="submit" name="changecat" value="submit" />
    </P>
    </form>

    <ol>
    <?php
    else: //display normal page
    //connect to the database server
    $dbcnx = @mysql_connect("localhost","root","xxxxxxx");
    if (!
    'dbcnx') {
        echo(
    "<p>Unable to connect to the ".
            
    "database server at this time.</p>");
        exit();
    }
    if (! @
    mysql_select_db("stock")){
        echo(    
    "<p>Unable to locate the stock ".
                
    "database at this time.</p>");
        exit();
    }
    //if stock item has been added
    //add it to the database.
    if (isset($_POST['entercat']) == "submit") {
    $sql "INSERT INTO categories SET
            Category='
    $_POST[category]'";
        if(@
    mysql_query($sql)) {
            echo(
    "<p>Your category has been added.</P>");
        }else {
            echo(
    "<p>Error adding category: ".
                    
    mysql_error()."</p>");
        }
    }
    //delete all stocklookup entries for the category
    //along with the entry for the category
    if (isset($_GET['removecat'])) {
    $ok1 = @mysql_query("DELETE FROM stocklookup WHERE CID=$_GET[removecat]");
    $ok2 = @mysql_query("DELETE FROM categories WHERE ID=$_GET[removecat]");
        if (
    $ok1 and $ok2){
        echo(
    "<p>The category has been removed.</p>");
        } else {
        echo(
    "<p>Error removing category:" mysql_error(). "</p>");
        }
    }
    //if stock item has been added
    //add it to the database.
    if (isset($_POST['changecat']) == "submit") {
    $sql "UPDATE categories SET
            Category='
    $_POST[category]' WHERE ID=$_POST[id]";
        if(@
    mysql_query($sql)) {
            echo(
    "<p>Your category has been amended.</P>");
        }else {
            echo(
    "<p>Error amending category: ".
                    
    mysql_error()."</p>");
        }
    }

    echo(
    "<p>Here are all the categories:"."</p>");

     
    //request the text of all the categories
    $result = @mysql_query("SELECT ID, Category FROM categories");    if(!$result) {
            echo(    
    "<p>Error performing query:" .mysql_error().                "</p>");
            exit();
    }
    //display the text of each category in a paragraph
    While ($row mysql_fetch_array($result)) {
        
    $catid $row["ID"];
        
    $category htmlspecialchars($row["Category"]);
        echo(
    "<li>$category "."[<a href='$_SERVER[PHP_SELF]?removecat=$catid'>Remove</a> |"." <a href='$_SERVER[PHP_SELF]?amendcat=$catid&cat=$category'>Amend</a>]"."</li>");
    }
    //When clicked, this link will load this page with the 
    //category submission form displayed.
            
    echo("<p><a href='$_SERVER[PHP_SELF]?addcat=1'>Add a new category</a></p>");

        endif;

    ?>
    </ol>
    <a href="admin.html">Return to main menu</a>
    </body>
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •