MySQL/PHP Form Help Please - Adding Numbers, etc.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Airdog
Forum Newbie
Posts: 2
Joined: Fri Oct 31, 2003 11:00 am

MySQL/PHP Form Help Please - Adding Numbers, etc.

Post by Airdog »

Hello and sorry if what I'm asking for might be a bit unclear, but I think you'll understand my explanation.

The webpage that I have is for an online XBox Live hockey league. Basically it keeps stats, etc. etc. What I want this script to do is accept stats submitted after a game and add them to the previous stats. So basically when a user inputs 1 goal, I want it to add that goal to the previous goal total. This problem is really holding up my development and will solve many more problems in my site.

This code will show all of the players on Toronto and Boston and display them in a table where they can all be edited and saved.

Sorry if I've posted too much code but I figured it was all pretty neccessary.

Code: Select all

$query="SELECT * FROM players WHERE Team='Toronto' OR Team='Boston' ORDER BY Team";
  $result=mysql_query($query);
  echo "<center><h1> Game Submission Testing </h1></center>";
  if (mysql_num_rows($result) !=0) { 
    echo "<table width=75% align=center>\n";
    echo "<form method=POST>\n";
    echo "<tr><td>Team</td><td>Name</td><td>Position</td><td>GP</td><td>Goals</td><td>Assists</td><td>SHG</td><td>PPG</td><td>PIM</td></tr>\n";
while ($r = mysql_fetch_array($result)) {
      $id = $r["id"];
      $Team = $r["Team"];
      $Name = $r["Name"];
      $Position = $r["Position"];
      $Goals = $r["Goals"];
      $Assists = $r["Assists"];
      $SHG = $r["SHG"];
      $PPG = $r["PPG"];
      $PIM = $r["PIM"];
      $GP = $r["GP"];
      

      echo "<input type=hidden name=yapp[$id] value='$id'>\n";
      echo "<tr><td><input type=text readonly name=yapp[$id][Team] value='$Team' size=20></td>\n";
      echo "<td><input type=text readonly name=yapp[$id][Name] value='$Name' size=20></td>\n";
      echo "<td><input type=text readonly name=yapp[$id][Position] value='$Position' size=2></td>\n";
      echo "<td><input type=text name=yapp[$id][GP] value='$GP' size=2></td>\n";
      echo "<td><input type=text name=yapp[$id][Goals] value='$Goals' size=2></td>\n";
      echo "<td><input type=text name=yapp[$id][Assists] value='$Assists' size=2></td>\n";
      echo "<td><input type=text name=yapp[$id][SHG] value='$SHG' size=2></td>\n";
      echo "<td><input type=text name=yapp[$id][PPG] value='$PPG' size=2></td>\n";
      echo "<td><input type=text name=yapp[$id][PIM] value='$PIM' size=2></td></tr>\n";
      
    }
    echo "<tr><td align=center colspan=8><input type=submit name=submit value=submit></td></tr>\n";
    echo "</form>";    
    echo "</table>";
  } else {
    echo "Couldn't fetch info from the db...";
  }
} else {
  
  $db=mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('I cannot connect to the database because: ' . mysql_error());
  mysql_select_db ("rsmeaton_players",$db);
  
  while (list ($key) = each ($HTTP_POST_VARS[yapp])) {
    $query="UPDATE players SET "; 
    foreach ($HTTP_POST_VARS[yapp][$key] as $key1 => $value1) {
      if (!empty($value1)) { 
        $query.="$key1='$value1', ";
      }
    }
    $query=substr($query, 0,-2);
    $query.=" WHERE id=$key";
    $result=mysql_query($query) or die (mysql_error()); 
  }
}
Thanks for any help given.
Airdog
Forum Newbie
Posts: 2
Joined: Fri Oct 31, 2003 11:00 am

Post by Airdog »

Anybody have any help?
hibbard
Forum Newbie
Posts: 11
Joined: Sun Nov 02, 2003 1:15 pm

Post by hibbard »

i'd say all you would have to do is call a simple "select <fieldname> from <table name> where username = $user (or whatever variable you use to hold that username ).

then, just <field name> + $whatever_variable_name_from_user_input

then, update the table.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Using match is not that hard in sql. You can use the field as the value it is, and increase (or decreas, multiply, whatever) it at the same time.
Take the following example (hope its understandable enough)

Code: Select all

-- imagine foo = 0
 update table set foo = foo + 1 where bar = 'cow'
 -- foo is now 1
 update table set foo = foo + 20 where bar = 'cow'
 -- foo is now 21

 -- in your case, imaginary code
 update table set goals = goals + $variable where id = $othervariable
Post Reply