Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2004
    Posts
    9

    Unanswered: Passing variables between 2 forms

    I have a code which I want to use to update a database of results from different soccer leagues.

    The first form has 2 drop down menus which select the league and the matchday (the variables tc and tj).

    <form action="updatemultiplerows.php" method="post">
    <SELECT NAME="tc">
    <OPTION VALUE=0>Competition
    <?=$options?>
    </SELECT></p>
    <SELECT NAME="tj">
    <OPTION VALUE=0>Matchday
    <?=$options3?>
    </SELECT></p>
    <p><input type="submit" name="submit" value="Select" /></p>
    <input type="hidden" name="submitted" value="TRUE"/>
    </form>
    I select them and it displays the matches from the selected league and matchday, with a 2 text boxes where I introduce the scores.

    When I submit this information it loads the second form with the following code.

    $sql="SELECT mid,hometeam, homescore, awayscore, awayteam FROM matches WHERE comp ='{$_POST['tc']}'
    AND matchday ='{$_POST['tj']}' ";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);
    ?>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="updatemultiplerowsBU.php">
    <tr>
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">


    <tr>
    <td align="center"><strong>HT</strong></td>
    <td align="center"><strong>HS</strong></td>
    <td align="center"><strong>AS</strong></td>
    <td align="center"><strong>AT</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $mid[]=$rows['mid']; ?><? echo $rows['mid']; ?></td>

    <td align="center"><input name="hometeam[]" type="text" id="name" value=" <? echo $rows['hometeam']; ?>"></td>

    <td align="center"><input name="homescore[]" type="text" id="homescore" value=" <? echo $rows['homescore']; ?>"></td>
    <td align="center"><input name="awayscore[]" type="text" id="awayscore" value="<? echo $rows['awayscore']; ?>"></td>
    <td align="center"><input name="awayteam[]" type="text" id="lastname" value="<? echo $rows['awayteam']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Submit2" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    <?php
    if($Submit2){
    for($i=0;$i<$count;$i++){
    $sql1="UPDATE matches SET homescore=($homescore[$i]), awayscore=($awayscore[$i]) WHERE mid='$mid[$i]'";
    $result1=mysql_query($sql1);
    }
    }
    The problem is that when I execute the query, it runs but returns no results, the 2 variables tc and tj are not recognised.

    Any ideas on what I am doing wrong?

  2. #2
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    If the second code you posted is the action= target of the first form, then the two variables are probably working. I assume that you have troubleshooted by echoing/printing out the $_POST variables -
    PHP Code:
    echo "<pre>";
    print_r($_POST);
    echo 
    "</pre>"
    If the second code you posted is ALL of your code, it is not connecting to the mysql server and it is not selecting a database. Also, doing some error checking and error reporting on your function calls would help in troubleshooting -
    PHP Code:
    $result=mysql_query($sql) or die('Query failed: ' mysql_error()); 
    It appears that your $sql query string is formed correctly, assuming that the $_POST variables contain the expected contents. Echoing your $sql variable to make sure it is formed correctly and has the expected contents is also a useful troubleshooting tool.
    Last edited by dbmab; 10-03-06 at 19:17.

  3. #3
    Join Date
    Nov 2004
    Posts
    9
    Thanks for your reply.

    I have done what you suggest and everything works except that in the second form the variables (tc and tj) are not recognised in the query.

    When I do echo $sql; on the 2nd form it shows these variables as empty ('')

    Both forms work on their own - the problem seems to be passing the variables from the first to the second

  4. #4
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    I suspect that the second section of code you posted is not the action = target of the first form (updatemultiplerows.php). Please post ALL your code and list which piece is which file name.

  5. #5
    Join Date
    Nov 2004
    Posts
    9
    Thanks for your reply

    You are right the target of the first form was the first form itself (I was running the select query on the same page) and not the second form .

    So I have rewritten the first form "updatemultiplerows.php" to just have the form with the 2 drop down menus.

    Now when the 2nd form "updatemultiplerowsBU.php" is executed the select query runs ok, the matches are displayed in the table (so the posted variables have been recognised I presume) . However the update query does not give any results.

    If I write values for the variables the query executes without problems.



    <?
    $page_title = 'Update Matches A';
    require ('includes/mysqlconnect.php');
    $sql="SELECT compid, compname FROM competitions ORDER BY compname";
    $result=mysql_query($sql);
    $options="";
    while ($row=mysql_fetch_array($result))
    {$ci=$row["compid"];
    $cn=$row["compname"];
    $options.="<OPTION VALUE=\"$ci\">".$cn.'</OPTION>';
    }
    $sql="SELECT jorid, jorname FROM jornadas ORDER BY jorname";
    $result=mysql_query($sql);
    while ($row=mysql_fetch_array($result))
    {$ji=$row["jorid"];
    $jn=$row["jorname"];
    $options3.="<OPTION VALUE=\"$ji\">".$jn.'</OPTION>';
    }




    ?><h2>Select League Matchday</h2>
    <form action="updatemultiplerowsBU.php" method="post">
    <SELECT NAME="compname">
    <OPTION VALUE=0>Competition
    <?=$options?>
    </SELECT></p>
    <SELECT NAME="matchday">
    <OPTION VALUE=0>Matchday
    <?=$options3?>
    </SELECT></p>
    <p><input type="submit" name="submit" value="Select" /></p>
    <input type="hidden" name="submitted" value="TRUE"/>
    </form>

    <?
    include ('header.htm');
    mysql_close($link);
    ?>
    <?php
    $page_title = 'Update Matches B';

    require ('includes/mysqlconnect.php');

    $sql="SELECT mid,hometeam, homescore, awayscore, awayteam FROM matches WHERE comp ='{$_POST['compname']}'
    AND matchday ='{$_POST['matchday']}' ";

    $result=mysql_query($sql) or die('Query failed: ' . mysql_error()); ;

    $count=mysql_num_rows($result);
    ?>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="updatemultiplerowsBU.php">
    <tr>
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">


    <tr>
    <td align="center"><strong>HT</strong></td>
    <td align="center"><strong>HS</strong></td>
    <td align="center"><strong>AS</strong></td>
    <td align="center"><strong>AT</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $mid[]=$rows['mid']; ?><? echo $rows['mid']; ?></td>

    <td align="center"><input name="hometeam[]" type="text" id="name" value=" <? echo $rows['hometeam']; ?>"></td>

    <td align="center"><input name="homescore[]" type="text" id="homescore" value=" <? echo $rows['homescore']; ?>"></td>
    <td align="center"><input name="awayscore[]" type="text" id="awayscore" value="<? echo $rows['awayscore']; ?>"></td>
    <td align="center"><input name="awayteam[]" type="text" id="lastname" value="<? echo $rows['awayteam']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    <?php
    // Check if button name "Submit" is active, do this
    if($Submit){
    for($i=0;$i<$count;$i++){
    $sql1="UPDATE matches SET homescore=($homescore[$i]), awayscore=($awayscore[$i]) WHERE mid='$mid[$i]'";
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    $result1=mysql_query($sql1) or die('Query failed: ' . mysql_error()); ;
    }
    }

    if($result1)
    {

    echo'<p> You have updated scores</p><p><br /></p>
    <p> <a href="updatemultiplerows.php"> UPDATE MORE</a>';
    include ('footer');
    exit();
    }

    else
    {
    echo'


    <p class="error">Nothings been updated beacause</p>';

    echo '<p>' . mysql_error() . '<br /><br />Query: '. $query .'</p>';

    include ('footer');
    exit();
    }

    mysql_close();


    ?>

  6. #6
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    The reason I asked the question was it appears that there was a missing step that was not shown.

    No variables carry over between PHP pages, even when a form action= is the same page the form code is on. Once the submit button is pressed, it is actually the bowser that sends the url for the page to go to, along with any $_POST/$_GET/$_FILE/cookie variables.

    For your current problem, your UPDATE query is within an if(...) conditional that is testing if a variable $Submit is true or false. Unless register globals are ON on your server, this variable does not exist because nothing in the code sets it.

  7. #7
    Join Date
    Nov 2004
    Posts
    9
    Thanks for explaining that.

    Is there anyway I can:

    a) Turn on the register globals

    or

    b) Adapt the scrip so that it will work with them off.

  8. #8
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    if you have access to the PHP.ini file then you can turn them on there
    I think its register globals
    you can also import the request variable

    for security purposes it best to explicitly refer to the variables as $_GET....etc
    however import request variables can add a prefix to make sure you dont breach the security
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

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