undefined index

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    undefined index

    I have a simple search feature that searches 3 fields in 3 separate tables
    in a MySQL db, and I'm getting an 'undefined index' error, but only in the
    first section (first table)of the results. The undefined index is tied to
    the docs.projID in the "if ($row_search['docs.projID'] == 1)". This if
    statement is there to control to format of the link as pages are in
    different depths of subfolders depending on security needs. Googling this
    sort of error seems to reflect problems with the $_POST['keyword'] not
    existing when the results are first listed, so I moved the PHP results code
    from it's own page (search.php) to the main page, which didn't help. I have
    tried several different things and can't seem to get it working right. Here
    is what I have:

    Form:
    <form id="search" name="search" method="post" action="<?php echo
    $currentPage?>" >
    SEARCH<br />
    <input name="keyword" type="text" size="20" value=""/>
    <input type="image" name="Submit"
    src="images/team_icon_go_bt n.gif" />
    </form>

    Results:

    <?php if (isset($_POST['search']))
    {
    $keyword = $_POST['keyword'];

    mysql_select_db ($database_webs ite, $website);
    $query_search = "SELECT Distinct docURL, docTitle, docDesc, docs.projID,
    groupCode, projCode, catName FROM docs
    JOIN subcat on subcat.subcatID =docs.subcatID
    JOIN cat on cat.catID = subcat.catID
    JOIN groups on groups.groupID= proj.groupID
    JOIN proj on proj.projID=doc s.projID
    WHERE proj.projID=doc s.projID AND docURL LIKE '%$keyword%' OR docTitle LIKE
    '%$keyword%' OR docDesc LIKE '%$keyword%'";
    $search = mysql_query($qu ery_search, $website) or die(mysql_error ());
    $row_search = mysql_fetch_ass oc($search);
    $totalRows_sear ch = mysql_num_rows( $search);
    $proj = $row_search['projCode'];
    ?>
    <?php if ($totalRows_sea rch < 1)
    {
    echo "No internal documents matched your request.";
    }

    else {?>

    <p><table width=95%>
    <tr><td colspan="3"><fo nt size="+1">Inter nal Documents:</font></td>
    </tr>
    <tr>
    <td><strongLi nk </strong></td>
    <td<strong>Desc ription</strong></td>
    <td<strong>Proj ect </strong></td>
    </tr>
    <?php
    do { ?>
    <tr>
    <td>
    <a href="<?php if ($row_search['docs.projID'] == 1) {
    echo $row_search['catName']."/".$row_sear ch['docURL'];
    }
    else {
    echo
    $row_search['groupCode']."/".$row_sear ch['projCode']."/".$row_sear ch['catName']."/".$row_sear ch['docURL'];
    ?>"><?php echo $row_search['docTitle'];
    } ?></a></td>
    <td><?php echo $row_search['docDesc']; ?></td><td><?php echo
    $row_search['projCode']; ?></td>
    </tr>
    <?php } while ($row_search = mysql_fetch_ass oc($search)); ?>

    </table>
    <?php }?>
    </p>
    <?php
    mysql_select_db ($database_webs ite, $website);
    $query_search = "SELECT Distinct toolURL, toolName, toolDesc, tools.projID,
    projCode FROM tools
    JOIN proj on proj.projID=too ls.projID WHERE toolURL LIKE '%$keyword%' OR
    toolName LIKE '%$keyword%' OR toolDesc LIKE '%$keyword%'";
    $search = mysql_query($qu ery_search, $website) or die(mysql_error ());
    $row_search = mysql_fetch_ass oc($search);
    $totalRows_sear ch = mysql_num_rows( $search);

    ?>
    <?php if ($totalRows_sea rch < 1)
    {
    echo "No tools matched your request.";
    }

    else {?>

    <p><table width=95%>
    <tr><td colspan="3"><fo nt size="+1">Tools :</font></td>
    </tr>
    <tr>
    <td><strongLi nk </strong></td>
    <td<strong>Desc ription</strong></td>
    <td<strong>Proj ect </strong></td>
    </tr>
    <?php do { ?>
    <tr>
    <td>
    <a href="<?php echo $row_search['toolURL']; ?>"><?php echo
    $row_search['toolName']; ?></a></td><td><?php echo $row_search['toolDesc'];
    ?></td><td><?php echo $row_search['projCode']; ?></td>
    </tr>
    <?php } while ($row_search = mysql_fetch_ass oc($search)); ?>

    </table>
    <?php }?>
    </p>

    <?php
    mysql_select_db ($database_webs ite, $website);
    $query_search = "SELECT Distinct pageURL, pageName, pageDesc, links.projID,
    projCode FROM links
    JOIN proj on proj.projID=lin ks.projID WHERE pageURL LIKE '%$keyword%' OR
    pageName LIKE '%$keyword%' OR pageDesc LIKE '%$keyword%'";
    $search = mysql_query($qu ery_search, $website) or die(mysql_error ());
    $row_search = mysql_fetch_ass oc($search);
    $totalRows_sear ch = mysql_num_rows( $search);

    ?>
    <?php if ($totalRows_sea rch < 1)
    {
    echo "No external documents matched your request.";
    }

    else {?>

    <p><table width=95%>
    <tr><td colspan="3"><fo nt size="+1">Exter nal Documents:</font></td>
    </tr>
    <tr>
    <td><strongLi nk </strong></td>
    <td<strong>Desc ription</strong></td>
    <td<strong>Proj ect </strong></td>
    </tr>
    <?php do { ?>
    <tr>
    <td>
    <a href="<?php echo $row_search['pageURL']; ?>"><?php echo
    $row_search['pageName']; ?></a></td><td><?php echo $row_search['pageDesc'];
    ?></td><td><?php echo $row_search['projCode']; ?></td>
    </tr>
    <?php } while ($row_search = mysql_fetch_ass oc($search)); ?>

    </table>
    <?php }?>
    </p>
    <?php
    mysql_free_resu lt($search);

    }


    ?>

    All guidance is welcome. Maybe I should move the if statement out of the
    link or assign a variable. I'm considering changing all of my links in the
    db to absolute URLs instead of relative, as this mapping to the location of
    a page has become a programming overload nightmare. Perhaps this would
    resolve my issue.

    Thanks,
    Christina


  • tomwerner@gmail.com

    #2
    Re: undefined index

    Hi,

    Technically undefined index errors should not affect your script,
    however, if you want to get rid of these warnings then make sure you
    initialise that index, the best way being assigning it a null value.

    Tom

    Comment

    • Chris

      #3
      Re: undefined index

      Thanks, but I don't quite understand. Wasn't the value (for all docs.projID
      rows) assigned when the query runs?

      $query_search = "SELECT Distinct docURL, docTitle, docDesc, docs.projID,
      groupCode, projCode, catName FROM docs
      JOIN subcat on subcat.subcatID =docs.subcatID
      JOIN cat on cat.catID = subcat.catID
      JOIN groups on groups.groupID= proj.groupID
      JOIN proj on proj.projID=doc s.projID
      WHERE proj.projID=doc s.projID AND docURL LIKE '%$keyword%' OR docTitle LIKE
      '%$keyword%' OR docDesc LIKE '%$keyword%'";

      Do you mean I should initialize a variable and give it a null value, then
      run the query?

      Christina

      <tomwerner@gmai l.comwrote in message
      news:1153250073 .720919.208950@ s13g2000cwa.goo glegroups.com.. .
      Hi,
      >
      Technically undefined index errors should not affect your script,
      however, if you want to get rid of these warnings then make sure you
      initialise that index, the best way being assigning it a null value.
      >
      Tom
      >

      Comment

      • Bent Stigsen

        #4
        Re: undefined index

        Chris wrote:
        I have a simple search feature that searches 3 fields in 3 separate tables
        in a MySQL db, and I'm getting an 'undefined index' error, but only in the
        first section (first table)of the results. The undefined index is tied to
        the docs.projID in the "if ($row_search['docs.projID'] == 1)".
        [snip]
        $query_search = "SELECT Distinct docURL, docTitle, docDesc, docs.projID,
        [snip]
        <a href="<?php if ($row_search['docs.projID'] == 1) {
        [snip]

        The name supplied by mysql would be that of the column, which would be
        "projID".

        Alternatively, you can give an alternate name in the select-statement:
        SELECT ... docs.projID `docs.projID`,


        --
        /Bent

        Comment

        • Eric Farraro

          #5
          Re: undefined index

          I dont know if youve tried this yet, but you might considering
          outputting the results of the query with the php function
          "print_r($query )".

          This will give you a "visual" picture of what is contained in your
          results (the array). I have often found out that the data I was
          looking for was actually there, but was not in the index I thought it
          was (for various reasons). Might be worth a try.

          Comment

          • Chris

            #6
            Re: undefined index

            Hi Tom,

            I don't understand how to initialize it. I have tried assigning it a null
            value, and other values. I have tried to use a different parameter for my
            if statement, but it also comes back as undefined index. I have added the
            proj.projID to the query, I have moved stuff around. I have added to,
            removed from and edited the query, changed things around, etc. So, what am
            I missing here? None of my other queries and code have this issue and they
            are done pretty much the same, other than having a 'keyword'. Code Repasted
            for another review (can someone point out the area that I'm not getting
            right?):

            <?php
            include("includ es/header.php");
            $keyword = $_POST['keyword'];

            mysql_select_db ($database_webs ite, $website);
            $query_search = "SELECT Distinct docURL, docTitle, docDesc, docs.projID,
            groupCode, projCode, catName FROM docs
            JOIN subcat on subcat.subcatID =docs.subcatID
            JOIN cat on cat.catID = subcat.catID
            JOIN groups on groups.groupID= proj.groupID
            JOIN proj on proj.projID=doc s.projID
            WHERE proj.projID=doc s.projID AND docURL LIKE '%$keyword%' OR docTitle LIKE
            '%$keyword%' OR docDesc LIKE '%$keyword%'";
            $search = mysql_query($qu ery_search, $website) or die(mysql_error ());
            $row_search = mysql_fetch_ass oc($search);
            $totalRows_sear ch = mysql_num_rows( $search);
            $proj = $row_search['projCode'];

            ?>
            <?php if ($totalRows_sea rch < 1)
            {
            echo "No internal documents matched your request.";
            }

            else {?>

            <p><table width=95%>
            <tr><td colspan="3"><fo nt size="+1">Inter nal Documents:</font></td>
            </tr>
            <tr>
            <td><strongLi nk </strong></td>
            <td<strong>Desc ription</strong></td>
            <td<strong>Proj ect </strong></td>
            </tr>
            <?php
            do { ?>
            <tr>
            <td>
            <a href="<?php if ($row_search['proj.projID'] == 1) {
            echo $row_search['catName']."/".$row_sear ch['docURL'];
            }
            else {
            echo
            $row_search['groupCode']."/".$row_sear ch['projCode']."/".$row_sear ch['catName']."/".$row_sear ch['docURL'];
            ?>"><?php echo $row_search['docTitle'];
            } ?></a></td>
            <td><?php echo $row_search['docDesc']; ?></td><td><?php echo
            $row_search['projCode']; ?></td>
            </tr>
            <?php } while ($row_search = mysql_fetch_ass oc($search)); ?>

            </table>
            <?php }?>
            </p>
            <?php
            mysql_select_db ($database_webs ite, $website);
            $query_search = "SELECT Distinct toolURL, toolName, toolDesc, tools.projID,
            projCode FROM tools
            JOIN proj on proj.projID=too ls.projID WHERE toolURL LIKE '%$keyword%' OR
            toolName LIKE '%$keyword%' OR toolDesc LIKE '%$keyword%'";
            $search = mysql_query($qu ery_search, $website) or die(mysql_error ());
            $row_search = mysql_fetch_ass oc($search);
            $totalRows_sear ch = mysql_num_rows( $search);

            ?>
            <?php if ($totalRows_sea rch < 1)
            {
            echo "No tools matched your request.";
            }

            else {?>

            <p><table width=95%>
            <tr><td colspan="3"><fo nt size="+1">Tools :</font></td>
            </tr>
            <tr>
            <td><strongLi nk </strong></td>
            <td<strong>Desc ription</strong></td>
            <td<strong>Proj ect </strong></td>
            </tr>
            <?php do { ?>
            <tr>
            <td>
            <a href="<?php echo $row_search['toolURL']; ?>"><?php echo
            $row_search['toolName']; ?></a></td><td><?php echo $row_search['toolDesc'];
            ?></td><td><?php echo $row_search['projCode']; ?></td>
            </tr>
            <?php } while ($row_search = mysql_fetch_ass oc($search)); ?>

            </table>
            <?php }?>
            </p>

            <?php
            mysql_select_db ($database_webs ite, $website);
            $query_search = "SELECT Distinct pageURL, pageName, pageDesc, links.projID,
            projCode FROM links
            JOIN proj on proj.projID=lin ks.projID WHERE pageURL LIKE '%$keyword%' OR
            pageName LIKE '%$keyword%' OR pageDesc LIKE '%$keyword%'";
            $search = mysql_query($qu ery_search, $website) or die(mysql_error ());
            $row_search = mysql_fetch_ass oc($search);
            $totalRows_sear ch = mysql_num_rows( $search);

            ?>
            <?php if ($totalRows_sea rch < 1)
            {
            echo "No external documents matched your request.";
            }

            else {?>

            <p><table width=95%>
            <tr><td colspan="3"><fo nt size="+1">Exter nal Documents:</font></td>
            </tr>
            <tr>
            <td><strongLi nk </strong></td>
            <td<strong>Desc ription</strong></td>
            <td<strong>Proj ect </strong></td>
            </tr>
            <?php do { ?>
            <tr>
            <td>
            <a href="<?php echo $row_search['pageURL']; ?>"><?php echo
            $row_search['pageName']; ?></a></td><td><?php echo $row_search['pageDesc'];
            ?></td><td><?php echo $row_search['projCode']; ?></td>
            </tr>
            <?php } while ($row_search = mysql_fetch_ass oc($search)); ?>

            </table>
            <?php }?>
            </p>
            <?php
            mysql_free_resu lt($search);
            include("includ es/footer.php"); ?>


            <tomwerner@gmai l.comwrote in message
            news:1153250073 .720919.208950@ s13g2000cwa.goo glegroups.com.. .
            Hi,
            >
            Technically undefined index errors should not affect your script,
            however, if you want to get rid of these warnings then make sure you
            initialise that index, the best way being assigning it a null value.
            >
            Tom
            >

            Comment

            • Jerry Stuckle

              #7
              Re: undefined index

              Chris wrote:
              I have a simple search feature that searches 3 fields in 3 separate tables
              in a MySQL db, and I'm getting an 'undefined index' error, but only in the
              first section (first table)of the results. The undefined index is tied to
              the docs.projID in the "if ($row_search['docs.projID'] == 1)". This if
              statement is there to control to format of the link as pages are in
              different depths of subfolders depending on security needs. Googling this
              sort of error seems to reflect problems with the $_POST['keyword'] not
              existing when the results are first listed, so I moved the PHP results code
              from it's own page (search.php) to the main page, which didn't help. I have
              tried several different things and can't seem to get it working right. Here
              is what I have:
              >
              <snip>

              Chris,

              What happens if you add the following:

              echo "<pre>\n";
              print_r($row_se arch);
              echo "</pre>\n";

              after your mysql_fetch_ass oc? It will tell you the each of the indexes
              and their contents.

              My suspicion is that 'docs.projID' is not a valid index. Probably 'projID'.

              You can also specify a name for it such as:

              $query_search = "SELECT Distinct docURL, docTitle, docDesc,
              docs.projID AS projID ...

              Then it will be $row_search['projID']


              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Miguel Cruz

                #8
                Re: undefined index

                "Chris" <designerNOSPAM @centurytel.net wrote:
                <a href="<?php if ($row_search['proj.projID'] == 1) {
                Jerry has hit it. You will never get an index with a dot in it from
                mysql_fetch_ass oc. You are wanting $row_search['projID'].

                miguel
                --
                Photos from 40 countries on 5 continents: http://travel.u.nu
                Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
                Airports of the world: http://airport.u.nu

                Comment

                • Chris

                  #9
                  Re: undefined index

                  >
                  What happens if you add the following:
                  >
                  echo "<pre>\n";
                  print_r($row_se arch);
                  echo "</pre>\n";
                  >
                  after your mysql_fetch_ass oc? It will tell you the each of the indexes
                  and their contents.
                  >
                  I get:

                  Array
                  (
                  [docURL] =index.php
                  [docTitle] =Debug Home
                  [docDesc] =Home page of Debug Tools team
                  [projID] =1
                  [groupCode] =common
                  [projCode] =Home
                  [catName] =General
                  )

                  If I take out the 'docs.' in the $row_search['docs.projID'] (or proj.projID)
                  my <a hreflinks don't work for docs in the '1' proj (projID which is
                  projCode 'Home'). When I change the query as noted below I also lose it.
                  If I assign a variable to the $row_search['docs.projID'] (or
                  proj.projID)>>> $projid = $row_search['proj'], then do the if statement
                  with the variable (if ($proj == 1) ), my links all work, but I get and
                  undefined variable error.

                  But I think we're on the right track. I have changed it back and forth a
                  few times and haven't given up yet. I may need to change the entire section
                  of code..as the code for other tables of links listed seem to work just
                  fine. But they don't have if statements - BTW, I have tried using other
                  operators with no luck. I've also used the projCode parameter (if
                  $row_search['projCode'] == 'Home') with no help.

                  Thanks,

                  Chris
                  My suspicion is that 'docs.projID' is not a valid index. Probably
                  'projID'.
                  >
                  You can also specify a name for it such as:
                  >
                  $query_search = "SELECT Distinct docURL, docTitle, docDesc,
                  docs.projID AS projID ...
                  >
                  Then it will be $row_search['projID']
                  >
                  >
                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: undefined index

                    Chris wrote:
                    >>What happens if you add the following:
                    >>
                    >>echo "<pre>\n";
                    >>print_r($row_ search);
                    >>echo "</pre>\n";
                    >>
                    >>after your mysql_fetch_ass oc? It will tell you the each of the indexes
                    >>and their contents.
                    >>
                    >
                    >
                    I get:
                    >
                    Array
                    (
                    [docURL] =index.php
                    [docTitle] =Debug Home
                    [docDesc] =Home page of Debug Tools team
                    [projID] =1
                    [groupCode] =common
                    [projCode] =Home
                    [catName] =General
                    )
                    >
                    If I take out the 'docs.' in the $row_search['docs.projID'] (or proj.projID)
                    my <a hreflinks don't work for docs in the '1' proj (projID which is
                    projCode 'Home'). When I change the query as noted below I also lose it.
                    If I assign a variable to the $row_search['docs.projID'] (or
                    proj.projID)>>> $projid = $row_search['proj'], then do the if statement
                    with the variable (if ($proj == 1) ), my links all work, but I get and
                    undefined variable error.
                    >
                    But I think we're on the right track. I have changed it back and forth a
                    few times and haven't given up yet. I may need to change the entire section
                    of code..as the code for other tables of links listed seem to work just
                    fine. But they don't have if statements - BTW, I have tried using other
                    operators with no luck. I've also used the projCode parameter (if
                    $row_search['projCode'] == 'Home') with no help.
                    >
                    Thanks,
                    >
                    Chris
                    >
                    Chris,

                    it needs to be $row_search['proj_id'] - just like it is in the output of
                    print_r().
                    >
                    >>My suspicion is that 'docs.projID' is not a valid index. Probably
                    >>'projID'.
                    >>
                    >>You can also specify a name for it such as:
                    >>
                    >>$query_sear ch = "SELECT Distinct docURL, docTitle, docDesc,
                    >>docs.projID AS projID ...
                    >>
                    >>Then it will be $row_search['projID']
                    >>
                    >>
                    >>--
                    >>============= =====
                    >>Remove the "x" from my email address
                    >>Jerry Stuckle
                    >>JDS Computer Training Corp.
                    >>jstucklex@att global.net
                    >>============= =====
                    >
                    >
                    >

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Christina

                      #11
                      Re: undefined index

                      Been there, done that, got the T-shirt - doesn't work.

                      Hmmmm....

                      "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
                      news:2bmdnZzbpc jPcFrZnZ2dnUVZ_ ridnZ2d@comcast .com...
                      Chris,
                      >
                      it needs to be $row_search['proj_id'] - just like it is in the output of
                      print_r().
                      >

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: undefined index

                        Christina wrote:
                        Been there, done that, got the T-shirt - doesn't work.
                        >
                        Hmmmm....
                        >
                        "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
                        news:2bmdnZzbpc jPcFrZnZ2dnUVZ_ ridnZ2d@comcast .com...
                        >
                        >
                        >>Chris,
                        >>
                        >>it needs to be $row_search['proj_id'] - just like it is in the output of
                        >>print_r().
                        >>
                        >
                        >
                        >
                        If your output from print_r() is correct, it will work. The fact it
                        doesn't means you have another problem with your syntax.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        Working...