#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    USA
    Posts
    226
    Rep Power
    15
    I am querying a database with several tables. I can get it to work with a ton of if then statements based on form input. I want to put the entire thing in a loop.

    Here is what it should do:
    Check to see what boxes have been checked on a form from the previous page. Query the database based on the selections made.

    I have the following code written, but keep getting this error:

    Warning: 0 is not a MySQL result index in /home/wbutyler/wbutyler-www/test.phtml on line 82

    Form Code:

    <form name="giftregistryform" action="test.phtml" method="post">
    <div align="center"><b><font color="Green"><font size="+2">Available Categories:</font></font></b><br><b>Select the product categories that you are interested in.</b></div><br>

    <table cellspacing="0" cellpadding="0" border="0">
    <tr>
    <td width="160">&nbsp;</td>
    <td>
    <input type="checkbox" name="choice1" value="Bird_Feeders"> Bird Feeders<br>
    <input type="checkbox" name="choice2" value="Tube_Feeders"> Finch and Tube Feeders<br>
    <input type="checkbox" name="choice3" value="Hummingbird_Feeders"> Hummingbird Feeders<br>
    <input type="checkbox" name="choice4" value="Specialty_Feeders"> Specialty Feeders<br>
    <input type="checkbox" name="choice5" value="Nesting"> Nesting<br>
    <input type="checkbox" name="choice6" value="Water"> Water<br>
    <input type="checkbox" name="choice7" value="Hardware"> Hardware<br>
    <input type="checkbox" name="choice8" value="Optics"> Optics<br>
    <input type="checkbox" name="choice9" value="Nature"> Nature Books and Videos<br><br>
    </td>
    </tr>
    </table>
    <div align="center"><input type="submit" value="View Products"><input type="reset" value="Reset"</div>
    </form>

    PHP Code:

    <?
    for($i = 1; $i <= 9; $i++)
    {
    if ($choice.$i <> "")
    {
    $dbLink = mysql_connect("localhost", "sample", "");

    mysql_select_db("sample", $dbLink);

    $Query = "SELECT ID, DESCRIPTION, PRICE, Image " . "FROM $choice.$i";
    $dbResult = mysql_query($Query, $dbLink);

    print("<tr><td colspan='5' align='center'><br>Bird Feeders</td></tr>");
    print("<tr><td><b>Quantity</b></td><td><b>ID</b></td><td><b>Description</b></td><td><b>Pri ce</b></td><td><b>Image</b></td></tr>");

    while($row = mysql_fetch_array($dbResult, MYSQL_ASSOC))
    {
    print("<tr bgcolor=".$color."><td align='center'><input type='text' size='2' name='$row[ID]'></td>" . "<td align='center'><font size='2'>" . $row[ID] . "</font></td>" . "<td><font size='2'>" . $row[DESCRIPTION] . "</font></td>" . "<td><font size='2'>$" . $row[PRICE] . "</font></td>" . "<td><font size='2'><img src='images/$row[Image]'>" . "</font></td></tr><tr><td colspan='5'><hr width='100%'></td></tr>n");
    }
    }
    }
    ?>

    As I said, it works perfectly without the for loop. What am I doing wrong????

    Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    114
    Rep Power
    15
    I can't tell from your post what value you assign to $choice, but I can see that $i is assigned an integer on each iteration of the for loop. So this means the conditional on your if statement will always be true, regardless of what's in $choice. Your saying $i=some_number, and then you ask does $i=something, well, yes it does. I think you should consider using an array instead of some difficult variable concatenation. Just say:
    <input type="checkbox" name="check[]" value="value 1">
    <input type="checkbox" name="check[]" value="value 2">
    <input type="checkbox" name="check[]" value="value 3">

    Your php script will automatically create an array based on the posted (selected) check boxes. So you could say.

    $j=count($check); // how many boxes were selected ?
    for ($i=0;$i<$j;$i++)
    {
    $sql="SELECT blah FROM blah WHERE blah=$check[$i]";
    // you should probably move the connect() out of the loop also as you only need it once.
    $result=mysql_query($sql);
    // code for result set...
    }

    So much less code that way. Be careful though, naming your selectboxes such[] makes php easier, but you'll need to refer to form controls with your javascript by their numeric index in the form array, eg, document.forms[0].elements[0] and not document.forms[0].check[] or check[0] or check0.

    additionally, you might want to try setting up your database so that you wouldn't have to make so many hits against it.

    [This message has been edited by billyo (edited September 26, 2000).]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    USA
    Posts
    226
    Rep Power
    15
    I tried it, but it still is not working. I really don't understand it. It looks like it should work perfectly. There has got to be something that I am overlooking.

    Again, the code is:

    Form:

    <input type="checkbox" name="choice[]" value="Bird_Feeders"> Bird Feeders<br>
    <input type="checkbox" name="choice[]" value="Tube_Feeders"> Finch and Tube Feeders<br>
    <input type="checkbox" name="choice[]" value="Hummingbird_Feeders"> Hummingbird Feeders<br>
    <input type="checkbox" name="choice[]" value="Specialty_Feeders"> Specialty Feeders<br>
    <input type="checkbox" name="choice[]" value="Nesting"> Nesting<br>
    <input type="checkbox" name="choice[]" value="Water"> Water<br>
    <input type="checkbox" name="choice[]" value="Hardware"> Hardware<br>
    <input type="checkbox" name="choice[]" value="Optics"> Optics<br>
    <input type="checkbox" name="choice[]" value="Nature"> Nature Books and Videos

    PHP:

    <?
    $dbLink = mysql_connect("localhost", "sample", "");

    mysql_select_db("sample", $dbLink);
    $j=count($choice);
    for ($i=0; $i<$j; $i++)
    {
    if ($choice[$i] <> "")
    {

    $Query = "SELECT ID, DESCRIPTION, PRICE" . "FROM $choice[$i]";
    $dbResult = mysql_query($Query, $dbLink);

    print("<tr><td colspan='4' align='center'><br>Bird Feeders</td></tr>");
    print("<tr><td><b>Quantity</b></td><td><b>ID</b></td><td><b>Description</b></td><td><b>Pri ce</b></td></tr>");

    while($row = mysql_fetch_array($dbResult, MYSQL_ASSOC))
    {
    print("<tr bgcolor=".$color."><td align='center'><input type='text' size='2' name='$row[ID]'></td>" . "<td align='center'><font size='2'>" . $row[ID] . "</font></td>" . "<td><font size='2'>" . $row[DESCRIPTION] . "</font></td>" . "<td><font size='2'>$" . $row[PRICE] . "</font></td>" . "</tr><tr><td colspan='4'><hr width='100%'></td></tr>n");
    }
    }
    }
    ?>

    It just is not looking through the individual tables. Each checkbox corresponds to a table in the database. Any more suggestions?

    I am also still receiving this error:
    Warning: 0 is not a MySQL result index in /home/wbutyler/wbutyler-www/test.phtml on line 83


    [This message has been edited by bramsey (edited September 26, 2000).]
  6. #4
  7. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    The error you are getting is telling you that Mysql did not return a match for your query, therefore PHP has nothing to work with.

    So, you're error at this point is just MySQL, not PHP.

    Perhaps your SQL query isn't quite right, or there is no data in the DB to match your query.

    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  8. #5
  9. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by bramsey:
    I tried it, but it still is not working. I really don't understand it. It looks like it should work perfectly. There has got to be something that I am overlooking.

    Again, the code is:

    Form:

    <input type="checkbox" name="choice[]" value="Bird_Feeders"> Bird Feeders<br>
    <input type="checkbox" name="choice[]" value="Tube_Feeders"> Finch and Tube Feeders<br>
    <input type="checkbox" name="choice[]" value="Hummingbird_Feeders"> Hummingbird Feeders<br>
    <input type="checkbox" name="choice[]" value="Specialty_Feeders"> Specialty Feeders<br>
    <input type="checkbox" name="choice[]" value="Nesting"> Nesting<br>
    <input type="checkbox" name="choice[]" value="Water"> Water<br>
    <input type="checkbox" name="choice[]" value="Hardware"> Hardware<br>
    <input type="checkbox" name="choice[]" value="Optics"> Optics<br>
    <input type="checkbox" name="choice[]" value="Nature"> Nature Books and Videos

    PHP:

    <?
    $dbLink = mysql_connect("localhost", "sample", "");

    mysql_select_db("sample", $dbLink);
    $j=count($choice);
    for ($i=0; $i<$j; $i++)
    {
    if ($choice[$i] <> "")
    {

    $Query = "SELECT ID, DESCRIPTION, PRICE" . "FROM $choice[$i]";
    $dbResult = mysql_query($Query, $dbLink);

    print("<tr><td colspan='4' align='center'><br>Bird Feeders</td></tr>");
    print("<tr><td><b>Quantity</b></td><td><b>ID</b></td><td><b>Description</b></td><td><b>Pri ce</b></td></tr>");

    while($row = mysql_fetch_array($dbResult, MYSQL_ASSOC))
    {
    print("<tr bgcolor=".$color."><td align='center'><input type='text' size='2' name='$row[ID]'></td>" . "<td align='center'><font size='2'>" . $row[ID] . "</font></td>" . "<td><font size='2'>" . $row[DESCRIPTION] . "</font></td>" . "<td><font size='2'>$" . $row[PRICE] . "</font></td>" . "</tr><tr><td colspan='4'><hr width='100%'></td></tr>n");
    }
    }
    }
    ?>

    It just is not looking through the individual tables. Each checkbox corresponds to a table in the database. Any more suggestions?

    I am also still receiving this error:
    Warning: 0 is not a MySQL result index in /home/wbutyler/wbutyler-www/test.phtml on line 83


    [This message has been edited by bramsey (edited September 26, 2000).]
    [/quote]

    Well.. actually I thin I have a really good idea where the problem is, by the way your
    sql statement is written, you are saying that "each" of you "choice[]" options is it's own table in your DB..

    If not, then you will have to re-work that SQL statement. To me, it doesn't seem like a normalized database would have a table for
    each individual product..

    But.. I don't know.. so.. I could be way off the beaten track here.. it would just seem to me that these items in the "choices[]" checkbox would be selected from like
    "products" using a "where" clause to select the specific product.

    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    32
    Rep Power
    15
    A way to supress this error (if you know you might not get any records) is to use @

    ex:

    $row = @mysql_fetch_array($result);

    The @ supress all error messages
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    114
    Rep Power
    15
    Yes, robert, that's what I thought too. bramsey, the if ($choice[$i]<>"") is really not necessary because the array only contains the selected values, that is, there are no empty or null elements in the array. Furthermore, it would be sufficeint to simply say if ($choice[$i]) { but even that is unwarranted in this case. The other thing could be the way you've concatenated your $query string... as: $Query = "SELECT ID, DESCRIPTION, PRICE" . "FROM $choice[$i]"; It would be evaluated as "SELECT ID, DESCRIPTION, PRICEFROM $choice[$i]"; You need a space in front of your FROM. PHP supports variable interpolation in double quoted strings, so you don't really need to use the concatenator "." here at all. But mostly, the PHP looks okay, you might want to make sure the queries themselves are okay by running them from the command line.

Similar Threads

  1. Trouble Ticket Tracking Software
    By thobbes867 in forum Dev Shed Lounge
    Replies: 12
    Last Post: May 14th, 2008, 12:21 PM
  2. array splitting trouble
    By dave2k in forum PHP Development
    Replies: 3
    Last Post: October 5th, 2003, 11:02 AM
  3. Having trouble in adding 1 to a database column
    By tom-hope in forum PHP Development
    Replies: 2
    Last Post: August 8th, 2003, 08:37 AM
  4. Sessions trouble.
    By GoBieN in forum PHP Development
    Replies: 7
    Last Post: August 5th, 2003, 10:16 PM
  5. trouble with patUser's modifyUser
    By tomhole in forum PHP Development
    Replies: 0
    Last Post: July 1st, 2003, 10:22 PM

IMN logo majestic logo threadwatch logo seochat tools logo