Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Help with Update script

    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
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Two things...

    First:
    PHP Code:
    <input type="hidden" name="id[]" value="<?php echo $databaseArray['name']; ?>">
    Do you really want to assign this hidden input field the value "name", and not "match_id"?

    Second:
    PHP Code:
    <?php while ($databaseArray mysql_fetch_array($databaseInfo)){ ?>
    <input name="nme[]" type="text" id="nme" value="<?php echo $databaseArray['name']?>" size="36" />
    <?php ?>
    This is going to create multiple <input> tags with the same "id" value. This is not valid HTML; always makes sure your HTML validates to avoid problems.


  •  

    Posting Permissions

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