SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update Script help?

    Hi Guys,

    Iím creating an update script, but am having one problem updating it, which is causing me a headache.

    Hereís my database with five rows:



    But when I try to update each row in the database via my php script below, it saves each record as the last entry entered (ie duplicates them):

    Can anyone help?

    Thanks

    Chris

    PHP Code:
    <?php

    // Get the data from the league table
    $databaseInfo mysql_query("SELECT * From player_stats") or die(mysql_error());

    // Update the new values in the database
    if(isset($_POST['Submit']))
    {

    for(
    $i 0$i count($_REQUEST['nme']) ; $i++)
    {
      
    $result mysql_query("Update player_stats set name ='".$_REQUEST['nme'][$i]."' WHERE match_id=".$_REQUEST['id'][$i]) OR DIE(mysql_error());
    }

    for(
    $i 0$i count($_REQUEST['gls']) ; $i++)
    {
      
    $result mysql_query("Update player_stats set goals ='".$_REQUEST['gls'][$i]."' WHERE match_id=".$_REQUEST['id'][$i]) OR DIE(mysql_error());
      
    header("Location: ../index.php"); 
    }

    //end of post

    ?>
    <form name="table" method="post" action="">
            <table width="100%" cellpadding="1" cellspacing="1">
              <tr> 
                <td width="39%">Position</td>
                <td width="7%">Name</td>
                <td width="9%">Goals</td>
              </tr>
              <?php while ($databaseArray mysql_fetch_array($databaseInfo)){ ?>
              <tr> 
                <td> <input type="hidden" name="id[]" value="<?php echo $databaseArray['name']; ?>"> 
                <?php echo $databaseArray['position']; ?></td>
                <td><input name="nme[]" type="text" id="nme" value="<?php echo $databaseArray['name']?>" size="36" /></td>
                <td><input name="gls[]" type="text" id="gls" value="<?php echo $databaseArray['goals']?>" size="36" /> 
                </td>
              </tr>
              <?php // end of while loop ?>
            </table>
    <input type="submit" name="Submit" value="Submit" class="button">
    </form>
    </body>
    </html>

  2. #2
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're missing the id/name of the player in your statement.
    And don't redirect in the second foreach()!

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    But I don't believe I need that (I may be wrong here) and Why not re-direct?

    Thanks

    Chris

  4. #4
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhm...I thought you wanted to set the goals per player, right?
    If you don't specify the players name the update sets it for _every_ player with the corresponding match_id.

    And the redirect "kills" your for() after the first run.
    Alle the other rounds until $i < count($_REQUEST['gls']) will never get executed.


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
  •