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)

    Problem updating values

    Morning All,

    Following a couple of days where I drove myself mad with a script, Iím trying to simplify my script and create something else that I would appreciate help with. Here's the scenario:

    I'm building a site for statistical data for a five aside football team. So my team has five players

    - goalkeeper
    - player_two
    - player_three
    - player_four
    - player_five

    and each match we play in is given a distinct match_id. Now for each player I record the following data:

    position (ie goalkeeper, player_two)
    name
    goals

    and then this data is nicely saved into a database as so:



    And hereís the code for this, which works great:

    PHP Code:
    <?php

    if(isset($_POST['Submit']))
    {
      
    $match_id=mysql_insert_id();
      if(!isset(
    $_GET['match_id']))
      { 
    $match_id $_GET['match_id'];
        foreach(
    $_REQUEST['r'] as $position => $row
        
      {   
    $row implode("', '",$row);
          
    $result mysql_query("Insert into player_stats(position,name,goals,match_id)   
                                 values('
    $position','$row','$match_id')");
      }
      }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Player stats</title>
    </head>

    <body>
    <form name="statistics" method="post" action="">
    <table width="100%">
      <tr>
        <td width="33%">Player</td>
        <td width="33%">Name</td>
        <td width="33%">Goals</td>
      </tr>
      <tr>
        <td width="33%">Goalkeeper:</td>
        <td width="33%"><input name="r[goalkeeper][name]" type="text"></td>
        <td width="33%"><input name="r[goalkeeper][goals]" type="text" /></td>
      </tr>
      <tr>
        <td width="33%">Player Two</td>
        <td width="33%"><input name="r[player_two][name]" type="text" /></td>
        <td width="33%"><input name="r[player_two][goals]" type="text" /></td>
      </tr>
      <tr>
        <td width="33%">Player Three</td>
        <td width="33%"><input name="r[player_three][name]" type="text" /></td>
        <td width="33%"><input name="r[player_three][goals]" type="text" /></td>
      </tr>
      <tr>
        <td width="33%">Player Four</td>
        <td width="33%"><input name="r[player_four][name]" type="text" /></td>
        <td width="33%"><input name="r[player_four][goals]" type="text" /></td>
      </tr>
      <tr>
        <td width="33%">Player Five</td>
        <td width="33%"><input name="r[player_five][name]" type="text" /></td>
        <td width="33%"><input name="r[player_five][goals]" type="text" /></td>
      </tr>
    </table>
    <input type="submit" name="Submit" value="Submit"/>
    </form>
    </body>
    </html>
    But hereís my problem. Say I enter a players name wrong, or the amount of goals he scored incorrect I want to be able to edit that data. So Iíd like to:

    - Grab all the details in the database where (get) .com?match_id=1 (as an example).
    - Put the values back in their corresponding textboxes on my form.
    - Then modify their values and hit submit which updates accordingly in the MYSQL database.

    Now Iíve tried, and think Iím partically there, but got stuck (see below). Can anyone help?

    Thanks

    PHP Code:
    <?php

    // Get the data where report id=get value at url
    if(isset($_GET['report_id']))

    {
      
    $result mysql_query("Select * From player_stats where report_id=".$_GET['report_id']);
      
    $row mysql_fetch_array($result);
      
    $name $row['name'];
      
    $goals $row['goals'];
      }

    // Submit the data using foreach
    if(isset($_POST['Submit']))
    {
      
    $match_id=mysql_insert_id();
      if(isset(
    $_GET['match_id']))
      { 
    $match_id $_GET['match_id'];
        foreach(
    $_REQUEST['r'] as $position => $row
        
      {   
    $row implode("', '",$row);
          
    $result mysql_query("UPDATE player_stats SET(position,name,goals,match_id)   
                                 values('
    $position','$row','$match_id' where report_id=".$_GET['report_id']);
      }
      }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Player stats</title>
    </head>

    <body>
    <form name="statistics" method="post" action="">
    <table width="100%">
      <tr>
        <td width="33%">Player</td>
        <td width="33%">Name</td>
        <td width="33%">Goals</td>
      </tr>
      <tr>
        <td width="33%">Goalkeeper:</td>
        <td width="33%"><input name="r[goalkeeper][name]" type="text" value="<?php echo $name?>" size="36" /></td>
        <td width="33%"><input name="r[goalkeeper][goals]" type="text" value="<?php echo $goals?>" size="36" /></td>
      </tr>
      <tr>
        <td width="33%">Player Two</td>
        <td width="33%"><input name="r[player_two][name]" type="text" value="<?php echo $name?>" size="36" /></td>
        <td width="33%"><input name="r[player_two][goals]" type="text" value="<?php echo $goals?>" size="36" /></td>
      </tr>
      <tr>
        <td width="33%">Player Three</td>
        <td width="33%"><input name="r[player_three][name]" type="text" value="<?php echo $name?>" size="36" /></td>
        <td width="33%"><input name="r[player_three][goals]" type="text" value="<?php echo $goals?>" size="36" /></td>
      </tr>
      <tr>
        <td width="33%">Player Four</td>
        <td width="33%"><input name="r[player_four][name]" type="text" value="<?php echo $name?>" size="36" /></td>
        <td width="33%"><input name="r[player_four][goals]" type="text" value="<?php echo $goals?>" size="36" /></td>
      </tr>
      <tr>
        <td width="33%">Player Five</td>
        <td width="33%"><input name="r[player_five][name]" type="text" value="<?php echo $name?>" size="36" /></td>
        <td width="33%"><input name="r[player_five][goals]" type="text" value="<?php echo $goals?>" size="36" /></td>
      </tr>
    </table>
    <input type="submit" name="Submit" value="Submit"/>
    </form>
    </body>
    </html>

  2. #2
    SitePoint Evangelist ldivinag's Avatar
    Join Date
    Jan 2005
    Location
    N37 33* W122 3*
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should check each MYSQL_QUERY call to make sure the RESULT is good.

    otherwise, write some code to display error and errno...
    leo d.

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

    But I know my query is wrong and won't work. I'm posting an example of how I think it may work, but would appreciate some help.

    Thanks

    Chris

  4. #4
    SitePoint Evangelist ldivinag's Avatar
    Join Date
    Jan 2005
    Location
    N37 33* W122 3*
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    remember $row is an array.

    so when you UPDATE
    Code:
    UPDATE player_stats SET(position,name,goals,match_id)   
    
                                 values('$position','$row','$match_id' where report_id=".$_GET['report_id'])
    the values $position, $row...

    that dumps the entire array, instead of using $name...
    leo d.


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
  •