Getting Values from database with php/html form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patsman77
    New Member
    • Nov 2007
    • 27

    Getting Values from database with php/html form

    I hope this is the right spot to post this....

    I am working on a form to pull the information from the database. I am trying to use arrays, but I only get one record to come back and it is duplicated in both rows on my form.

    the code is:

    [PHP]<?php $sql="select *from admin Where Week='1'";
    $result=mysql_q uery($sql);
    $num=mysql_numr ows($result);
    $i=0;
    while ($i < $num)
    {$pass[GameID]=mysql_result($ result,$i,"Game ID");
    $pass[Week]=mysql_result($ result,$i,"Week ");
    $pass[Favorite]=mysql_result($ result,$i,"Favo rite");
    $pass[Underdog]=mysql_result($ result,$i,"Unde rdog");
    $pass[Spread]=mysql_result($ result,$i,"Spre ad");
    $pass[FavScore]=mysql_result($ result,$i,"FavS core");
    $pass[UndScore]=mysql_result($ result,$i,"UndS core");
    $pass[Time]=mysql_result($ result,$i,"Time ");
    $i++;
    }mysql_close();
    ?>[/PHP]

    [HTML]
    <form action="adminin sert.php" method="POST">

    <Select size="1" name="Week" ><option value="None" <? if($pass[$Week]== "None"){ echo "selected"; } ?>>Week</option>
    <option <? if($pass[Week]== "01" ) { echo "selected"; } ?>>01</option>
    <option <? if($pass[Week]== "02" ) { echo "selected"; } ?>>02</option>

    <li>
    <?php echo"$pass[GameID]"; ?>

    <select size="1" name="Favorite[]"><option value="None"<? if($pass[Favorite]== "None"){ echo "selected"; } ?>>Favorite</option>
    <option <? if($pass[Favorite]== "Atlanta"){ echo "selected"; } ?>>Atlanta</option><option <? if($pass[Favorite]== "Arizona"){ echo "selected"; } ?>>Arizona</option>

    <select size="1" name="Underdog[]"><option value="None"<? if($pass[Underdog]== "None"){ echo "selected"; } ?>>Underdog</option>
    <option <? if($pass[Underdog]== "Atlanta"){ echo "selected"; } ?>>Atlanta</option>
    <option <? if($pass[Underdog]== "Arizona"){ echo "selected"; } ?>>Arizona</option>

    <input type="Number" name="Spread[]" size="5" MAXLENGTH="4" value="<? if($pass[Spread]!=""){ echo "$pass[Spread]"; } ?>" >

    <input type="Number" name="FavScore[]" size="5" MAXLENGTH="4" value="<? if($pass[FavScore]!=""){ echo "$pass[FavScore]"; } ?>" >

    <input type="Number" name="UndScore[]" size="5" MAXLENGTH="4" value="<? if($pass[UndScore]!=""){ echo "$pass[UndScore]"; } ?>" >

    <input type="Number" name="Time[]" size="5" MAXLENGTH="8" value="<? if($pass[Time]!=""){ echo "$pass[Time]"; } ?>" >
    [/HTML]

    with the second part of the html code duplicated...

    am i on right track, what am i missing to get it to pull each row?

    Any help would be great,

    Patsman77
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Patsman.

    You may find it a bit more economical to do this:
    [code=php]
    $sql = "SELECT * FROM `admin` WHERE `Week` = '1'";
    $result = mysql_query($sq l);

    $pass = array();
    while( $row = mysql_fetch_ass oc($result) )
    {
    $pass[] = $row;
    }
    mysql_free_resu lt($result);
    [/code]

    Comment

    • patsman77
      New Member
      • Nov 2007
      • 27

      #3
      Ok, I changed the code with what you suggested, and I get no values carried onto my form from database....... just comes up with default values as if I entered nothing??

      I should be replacing everything from the select statement down to the nysql close line....am I right in thinking this?

      Thanx,

      Patsman77

      Btw, thanx for taking the time to answer my post!

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Patsman.

        In your form, instead of using echo "$pass[Spread]", etc., use echo "$pass[0][Spread]".

        Comment

        • patsman77
          New Member
          • Nov 2007
          • 27

          #5
          Hello Again,

          I use this line:

          [PHP]echo "<td> <input type="Number" name="Week" size="5" MAXLENGTH="4" value="<? if($pass[0][Week]!=""){ echo "$pass[0][Week]"; } ?>" ></td>" ;[/PHP]

          I get this error:

          Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 71

          which is the line mentioned...

          Thanx for any help

          Patsman77

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Patsman.

            To use a multi-dimensional array in a string, you must use curly syntax:

            [code=php]
            echo "<td> <input type="Number" name="Week" size="5" MAXLENGTH="4" value="<? if($pass[0][Week]!=""){ echo "{$pass[0][Week]}"; } ?>" ></td>" ;
            [/code]

            Alternatively (since PHP will auto-cast it to a string anyway):
            [code=php]
            echo "<td> <input type="Number" name="Week" size="5" MAXLENGTH="4" value="<? if($pass[0][Week]!=""){ echo $pass[0]['Week']; } ?>" ></td>" ;
            [/code]

            [EDIT: Oh... and you'll need to escape your quotes or use single quotes to enclose the string:
            [code=php]
            echo '<td> <input type="Number" name="Week" size="5" MAXLENGTH="4" value="';
            if( ! empty($pass[0][Week]) )
            {
            echo $pass[0]['Week'];
            }
            echo '" /></td>';
            [/code]
            ]
            Last edited by pbmods; Nov 20 '07, 03:51 AM.

            Comment

            • patsman77
              New Member
              • Nov 2007
              • 27

              #7
              Well, i am getting further and further, I think with a little more assistance, I will finally be there. Sorry to ask for so much help, but I am in the learning process with this, especially arrays!

              I have everything edited correctly now I think except for the drop down box selections....I f you can assist me with the first, it should take of the second and I will be done! Hopefully that is!!

              So this part of the code??

              [PHP]echo '<select size="1" name="Favorite[]">'
              echo '<td> <option value="None"<? if($pass[0][Favorite]== "None"){ echo "selected"; } ?>>Favorite</option>';
              <option <? if($pass[Favorite]== "Atlanta"){ echo "selected"; } ?>>Atlanta</option>
              <option <? if($pass[Favorite]== "Arizona"){ echo "selected"; } ?>>Arizona</option>[/PHP]

              I took the quotes and changed them to ' , but that didnt help as I get the same error as before again....

              Thanx again for all your help!

              Patsman77

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Patsman.

                Try this on for size:
                [code=php]
                ?>

                <select size="1" name="Favorite[]">
                <option value="None" <?php if($pass[0][Favorite]== "None"){ echo "selected"; } ?>>Favorite</option>
                <option <?php if($pass[Favorite]== "Atlanta"){ echo "selected"; } ?>>Atlanta</option>
                <option <?php if($pass[Favorite]== "Arizona"){ echo "selected"; } ?>>Arizona</option>

                <?php
                [/code]

                Comment

                • patsman77
                  New Member
                  • Nov 2007
                  • 27

                  #9
                  Well, now it at least gets to the end of the code, but I get this error:

                  Parse error: parse error, unexpected $ in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 203

                  line 203 is:

                  </html>

                  ???? there is no $ there???

                  ThAnx,

                  Patsman77

                  Comment

                  • patsman77
                    New Member
                    • Nov 2007
                    • 27

                    #10
                    Here is what I got now:


                    [PHP]$sql = "SELECT * FROM `admin` WHERE `Week` = '1'";
                    $result = mysql_query($sq l);

                    $pass = array();
                    while( $row = mysql_fetch_ass oc($result) )
                    {
                    $pass[] = $row;
                    }
                    mysql_free_resu lt($result);

                    $pass[GameID]=mysql_result($ result,$i,"Game ID");
                    $pass[Week]=mysql_result($ result,$i,"Week ");
                    $pass[Favorite]=mysql_result($ result,$i,"Favo rite");
                    $pass[Underdog]=mysql_result($ result,$i,"Unde rdog");
                    $pass[Spread]=mysql_result($ result,$i,"Spre ad");
                    $pass[FavScore]=mysql_result($ result,$i,"FavS core");
                    $pass[UndScore]=mysql_result($ result,$i,"UndS core");
                    $pass[Time]=mysql_result($ result,$i,"Time ");







                    mysql_close();
                    ?>

                    <html>

                    <head>
                    <meta http-equiv="Content-Language" content="en-us">
                    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
                    <title>SwammiSp orts Admin Page</title>
                    </head>

                    <body bgcolor="#0099F F">

                    <p align="center"> <b><font size="6" face="Chocolate Box">SwammiSpor ts Admin Page</font></b></p>
                    <table border="0" width="100%" id="table2">
                    <tr>
                    <td><b><font color="#FFFFFF" face="Georgia"> Enter matchups and spreads
                    here</font></b></td>
                    </tr>
                    </table>

                    <form action="adminin sert.php" method="POST">





                    Enter current week:

                    <br>
                    <p>
                    <table><th>ID </th><th>WK</th><th>Favorite </th><th>Underdog </th><th>PS</th><th>FS</th><th>US</th><th>Time</th>
                    <?
                    $number = 16;
                    for ($i=1; $i<=$number; $i++)
                    {


                    echo "<tr>";
                    echo"$pass[GameID]";
                    echo '<td> <input type="Number" name="Week" size="5" MAXLENGTH="4" value="';
                    if( ! empty($pass[0][Week]) )
                    {
                    echo $pass[0]['Week'];
                    }
                    echo '" /></td>';
                    ?>

                    <select size="1" name="Favorite[]">
                    <option value="None" <?php if($pass[0][Favorite]== "None"){ echo "selected"; } ?>>Favorite</option>
                    <option <?php if($pass[Favorite]== "Atlanta"){ echo "selected"; } ?>>Atlanta</option>
                    <option <?php if($pass[Favorite]== "Arizona"){ echo "selected"; } ?>>Arizona</option>
                    <option <?php if($pass[Favorite]== "Baltimore" ){ echo "selected"; } ?>>Baltimore</option>
                    <option <?php if($pass[Favorite]== "Buffalo"){ echo "selected"; } ?>>Buffalo</option>
                    <option <?php if($pass[Favorite]== "Carolina") { echo "selected"; } ?>>Carolina</option>
                    <option <?php if($pass[Favorite]== "Chicago"){ echo "selected"; } ?>>Chicago</option>
                    <option <?php if($pass[Favorite]== "Cincinnati "){ echo "selected"; } ?>>Cincinnati</option>
                    <option <?php if($pass[Favorite]== "Cleveland" ){ echo "selected"; } ?>>Cleveland</option>
                    <option <?php if($pass[Favorite]== "Dallas"){ echo "selected"; } ?>>Dallas</option>
                    <option <?php if($pass[Favorite]== "Denver"){ echo "selected"; } ?>>Denver</option>
                    <option <?php if($pass[Favorite]== "Detroit"){ echo "selected"; } ?>>Detroit</option>
                    <option <?php if($pass[Favorite]== "Green Bay"){ echo "selected"; } ?>>Green Bay</option>
                    <option <?php if($pass[Favorite]== "Houston"){ echo "selected"; } ?>>Houston</option>
                    <option <?php if($pass[Favorite]== "Indianapolis") { echo "selected"; } ?>>Indianapolis </option>
                    <option <?php if($pass[Favorite]== "Jacksonville") { echo "selected"; } ?>>Jacksonville </option>
                    <option <?php if($pass[Favorite]== "Kansas City"){ echo "selected"; } ?>>Kansas City</option>
                    <option <?php if($pass[Favorite]== "Miami"){ echo "selected"; } ?>>Miami</option>
                    <option <?php if($pass[Favorite]== "Minnesota" ){ echo "selected"; } ?>>Minnesota</option>
                    <option <?php if($pass[Favorite]== "New England"){ echo "selected"; } ?>>New England</option>
                    <option <?php if($pass[Favorite]== "New Orleans"){ echo "selected"; } ?>>New Orleans</option>
                    <option <?php if($pass[Favorite]== "NY Giants"){ echo "selected"; } ?>>NY Giants</option>
                    <option <?php if($pass[Favorite]== "NY Jets"){ echo "selected"; } ?>>NY Jets</option>
                    <option <?php if($pass[Favorite]== "Oakland"){ echo "selected"; } ?>>Oakland</option>
                    <option <?php if($pass[Favorite]== "Philadelphia") { echo "selected"; } ?>>Philadelphia </option>
                    <option <?php if($pass[Favorite]== "Pittsburgh "){ echo "selected"; } ?>>Pittsburgh</option>
                    <option <?php if($pass[Favorite]== "San Diego"){ echo "selected"; } ?>>San Diego</option>
                    <option <?php if($pass[Favorite]== "San Francisco"){ echo "selected"; } ?>>San Francisco</option>
                    <option <?php if($pass[Favorite]== "Seattle"){ echo "selected"; } ?>>Seattle</option>
                    <option <?php if($pass[Favorite]== "St.Louis") { echo "selected"; } ?>>St.Louis</option>
                    <option <?php if($pass[Favorite]== "Tampa Bay"){ echo "selected"; } ?>>Tampa Bay</option>
                    <option <?php if($pass[Favorite]== "Tennessee" ){ echo "selected"; } ?>>Tennessee</option>
                    <option <?php if($pass[Favorite]== "Washington "){ echo "selected"; } ?>>Washington</option>
                    </select></td>






                    <select size="1" name="Underdog[]">
                    <option value="None" <?php if($pass[0][Underdog]== "None"){ echo "selected"; } ?>>Favorite</option
                    <option <? if($pass[Underdog]== "Atlanta"){ echo "selected"; } ?>>Atlanta</option>
                    <option <? if($pass[Underdog]== "Arizona"){ echo "selected"; } ?>>Arizona</option>
                    <option <? if($pass[Underdog]== "Baltimore" ){ echo "selected"; } ?>>Baltimore</option>
                    <option <? if($pass[Underdog]== "Buffalo"){ echo "selected"; } ?>>Buffalo</option>
                    <option <? if($pass[Underdog]== "Carolina") { echo "selected"; } ?>>Carolina</option>
                    <option <? if($pass[Underdog]== "Chicago"){ echo "selected"; } ?>>Chicago</option>
                    <option <? if($pass[Underdog]== "Cincinnati "){ echo "selected"; } ?>>Cincinnati</option>
                    <option <? if($pass[Underdog]== "Cleveland" ){ echo "selected"; } ?>>Cleveland</option>
                    <option <? if($pass[Underdog]== "Dallas"){ echo "selected"; } ?>>Dallas</option>
                    <option <? if($pass[Underdog]== "Denver"){ echo "selected"; } ?>>Denver</option>
                    <option <? if($pass[Underdog]== "Detroit"){ echo "selected"; } ?>>Detroit</option>
                    <option <? if($pass[Underdog]== "Green Bay"){ echo "selected"; } ?>>Green Bay</option>
                    <option <? if($pass[Underdog]== "Houston"){ echo "selected"; } ?>>Houston</option>
                    <option <? if($pass[Underdog]== "Indianapolis") { echo "selected"; } ?>>Indianapolis </option>
                    <option <? if($pass[Underdog]== "Jacksonville") { echo "selected"; } ?>>Jacksonville </option>
                    <option <? if($pass[Underdog]== "Kansas City"){ echo "selected"; } ?>>Kansas City</option>
                    <option <? if($pass[Underdog]== "Miami"){ echo "selected"; } ?>>Miami</option>
                    <option <? if($pass[Underdog]== "Minnesota" ){ echo "selected"; } ?>>Minnesota</option>
                    <option <? if($pass[Underdog]== "New England"){ echo "selected"; } ?>>New England</option>
                    <option <? if($pass[Underdog]== "New Orleans"){ echo "selected"; } ?>>New Orleans</option>
                    <option <? if($pass[Underdog]== "NY Giants"){ echo "selected"; } ?>>NY Giants</option>
                    <option <? if($pass[Underdog]== "NY Jets"){ echo "selected"; } ?>>NY Jets</option>
                    <option <? if($pass[Underdog]== "Oakland"){ echo "selected"; } ?>>Oakland</option>
                    <option <? if($pass[Underdog]== "Philadelphia") { echo "selected"; } ?>>Philadelphia </option>
                    <option <? if($pass[Underdog]== "Pittsburgh "){ echo "selected"; } ?>>Pittsburgh</option>
                    <option <? if($pass[Underdog]== "San Diego"){ echo "selected"; } ?>>San Diego</option>
                    <option <? if($pass[Underdog]== "San Francisco"){ echo "selected"; } ?>>San Francisco</option>
                    <option <? if($pass[Underdog]== "Seattle"){ echo "selected"; } ?>>Seattle</option>
                    <option <? if($pass[Underdog]== "St.Louis") { echo "selected"; } ?>>St.Louis</option>
                    <option <? if($pass[Underdog]== "Tampa Bay"){ echo "selected"; } ?>>Tampa Bay</option>
                    <option <? if($pass[Underdog]== "Tennessee" ){ echo "selected"; } ?>>Tennessee</option>
                    <option <? if($pass[Underdog]== "Washington "){ echo "selected"; } ?>>Washington</option>
                    </select></td>"




                    <?

                    echo '<td> <input type="Number" name="Spread" size="5" MAXLENGTH="4" value="';
                    if( ! empty($pass[0][Spread]) )
                    {
                    echo $pass[0]['Spread'];
                    }
                    echo '" /></td>';

                    echo '<td> <input type="Number" name="FavScore" size="5" MAXLENGTH="4" value="';
                    if( ! empty($pass[0][FavScore]) )
                    {
                    echo $pass[0]['FavScore'];
                    }
                    echo '" /></td>';


                    echo '<td> <input type="Number" name="UndScore" size="5" MAXLENGTH="4" value="';
                    if( ! empty($pass[0][UndScore]) )
                    {
                    echo $pass[0]['UndScore'];
                    }
                    echo '" /></td>';

                    echo '<td> <input type="Number" name="Time" size="5" MAXLENGTH="8" value="';
                    if( ! empty($pass[0][Time]) )
                    {
                    echo $pass[0]['Time'];
                    }
                    echo '" /></td>';


                    echo "</tr>";
                    }
                    ?>
                    </table>


                    <center>
                    <input type=submit name=submit value=Submit> </ul>
                    </ul>
                    </form>

                    </body>
                    </html> [/PHP]

                    and I get this error:

                    Warning: mysql_result(): 3 is not a valid MySQL result resource in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 16

                    Warning: mysql_result(): 3 is not a valid MySQL result resource in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 17

                    Warning: mysql_result(): 3 is not a valid MySQL result resource in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 18

                    Warning: mysql_result(): 3 is not a valid MySQL result resource in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 19

                    Warning: mysql_result(): 3 is not a valid MySQL result resource in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 20

                    Warning: mysql_result(): 3 is not a valid MySQL result resource in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 21

                    Warning: mysql_result(): 3 is not a valid MySQL result resource in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 22

                    Warning: mysql_result(): 3 is not a valid MySQL result resource in /home/swammisp/public_html/Mambo/swammiadmin2.ph p on line 23

                    Patsman77

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya, Patsman.

                      Sounds like your SQL query is generating an error. Try adding this line after mysql_query():

                      [code=php]
                      if( $err = mysql_error() )
                      {
                      echo $err;
                      exit;
                      }
                      [/code]

                      Comment

                      Working...