SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member Cas's Avatar
    Join Date
    Jun 2001
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    It works...but,with bugs

    Here is a slice of code that I have adapted a little from Kevin Yanks tutorial. I seem to get a ( Warning undefined variable cat2 ) in C: ect... I think the cat2 relates to the ID of paticular catagory in my catagory table. Everything despite these errors does work correctly I have done a bit of testing. However if I change ($$var) to ($var) I automaticly have ID's from all my catagories entered into my lookup table and if I only have one catagory in my catagory table eveything works fine i.e. no Warnings and no unsual behaviour.


    //--------
    $aid = mysql_insert_id();

    $cats = mysql_query("SELECT ID, Name FROM ad_categories");

    while ($cat = mysql_fetch_array($cats)) {
    $cid = $cat["ID"];
    $cname = $cat["Name"];
    $var = "cat$cid"; // The name of the variable
    if ($$var) { // The checkbox is checked
    $sql = "INSERT IGNORE INTO ad_lookup " .
    "SET AID=$aid, CID=$cid";
    $ok = mysql_query($sql);
    if ($ok) {
    echo("<P>Avertisment added to category: $cname</P>");
    } else {
    echo("<P>Error inserting advert into category $cname:" .
    mysql_error() . "</P>");
    }
    } // end of if ($$var)
    } // end of while loop
    ?>
    //--------





    There is also another problem that I have and I think could be very simple, athough I cant seem to solve It..
    I get a Undifined variable on every page in the tutorial that has an if ($submit) argument ("argument" is that the right terminology)
    The error is as follows..... Warning: Undefined Variable submit C: etc...

    a sample of code that's giving me grief.

    //------

    <!-- editcat.php -->
    <HTML>
    <HEAD>
    <TITLE> Edit Category </TITLE>
    </HEAD>
    <BODY>
    <?php

    $dbcnx = @mysql_connect("localhost", "root", "mypass");
    mysql_select_db("forces_web");

    if ($submit): // The category's details have
    // been updated.

    $sql = "UPDATE ad_categories SET " .
    "Name='$name' " .
    "WHERE ID=$id";
    if (mysql_query($sql)) {
    echo("<P>Category details updated.</P>");
    } else {
    echo("<P>Error updating category details: " .
    mysql_error() . "</P>");
    }

    ?>

    <P><A HREF="cats.php">Return to Category list</A></P>

    <?php
    else: // Allow the user to edit the category
    // with ID=$id

    $cat=mysql_query("SELECT Name " .
    "FROM ad_categories " .
    "WHERE ID=$id");
    if (!$cat) {
    echo("<P>Error fetching category details: " .
    mysql_error() . "</P>");
    exit();
    }

    $cat = mysql_fetch_array($cat);

    $name = $cat["Name"];

    // Add slashes to database
    // value for use as HTML attribute
    $name = addslashes($name);

    ?>

    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <P>Edit the categoryBR>
    Name: <INPUT TYPE=TEXT NAME="name" VALUE="<?php echo($name); ?>" SIZE=20 MAXLENGTH=100><BR>
    <INPUT TYPE=HIDDEN NAME="id" VALUE="<?php echo($id); ?>">
    <INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT"></P>
    </FORM>

    <?php endif; ?>

    </BODY>
    </HTML>
    //----------



    Thank you to anyone offering help ( I need it ),
    Thank you again,
    Cas
    Last edited by Cas; Jul 15, 2001 at 11:42.
    "Tell me and I will forget , show me and I'll learn.
    Involve me and I will remember. " Confucius.

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($submit):

    Should there not be a { instead of :

    Try this first

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you've got something set in your php.ini that is requiring variables to be declared before they can be used. I have forgot what this setting is.

    This is the sort of behaviour more type-safe languages such as C, C++, Java enforce. However, in the loose-and-breezy-one-size-fits-all world of web scripting, most people have php configured to allow variables to be initialised on the run, when they are first encountered in the script rather than being explicitly declared.

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, seanf,

    if (condition):
    // blah
    endif;

    is an alternate form of coding control structures in php:
    http://www.zend.com/manual/control-s...ive-syntax.php

    But there should be a closing
    endif;
    in the above code.

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I said "Should"

    Thank you freakysid!

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    SitePoint Member Cas's Avatar
    Join Date
    Jun 2001
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help,
    I've looked through the PHP ini file and I can't seem to find any particular reference to how variables should perform in PHP.
    I have also looked at the PHP manual to no avail. I'm sure It's there in both, but I'm not quite sure what I am looking for.

    Thanks again
    Cas.
    "Tell me and I will forget , show me and I'll learn.
    Involve me and I will remember. " Confucius.


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
  •