Jump to content

Recommended Posts

Hi there, Barand!  ;D

 

foreach ($_POST['hits'] as $playerID => $hit)

 

For example, I have $_POST['hits'], $_POST['ab'], $_POST['bb']. Each of these are arrays (containing the number of hits for each playerID, the number of at bats for each playerID, and the number of walks for each playerID). Each has its own column in my db table. playerID is also a column and I want to store the values for each playerID.

 

<?php
# inserting stats for new tourny
$player = $_POST['playerID'];
foreach ($player as $order => $playerID){
  $queryStatsPer = "
  INSERT into stats_per_tourny (playerID, tournyID, hit, ab, bb)
  VALUES ( '$playerID', '".$_POST['tournyID']."', '$hit', '$ab', '$bb'
)";
$insertStatsPer = mysql_query($queryStatsPer) or die("<p class='style5'>Insert query failed: " .  mysql_error()) . "</p>";
}
?>

 

Thanks in advance.

foreach ($_POST['playerID'] as $k => $playerID) {
           $hits = $_POST['hits'][$k];
           $ab = $_POST['ab'][$k];
           $bb = $_POST['bb'][$k];
           $queryStatsPer = "
               INSERT into stats_per_tourny (playerID, tournyID, hit, ab, bb)
               VALUES ( '$playerID', '".$_POST['tournyID']."', '$hits', '$ab', '$bb'
               )";
           $insertStatsPer = mysql_query($queryStatsPer) or die("<p class='style5'>Insert query failed: " .  mysql_error()) . "</p>";

}

You are continually awesome!

 

I'm trying to apply this idea when using operations. I'm doing a query to get values currently stored in a db table. I am adding the posted values to those and then doing an update query. Here is what I have so far:

 

<?php
while($all = mysql_fetch_array( $allstats )) 
{
  foreach ($player as $id => $playerID)
  {
    $hit = $_POST['hit'][$id];
    $allhit = $all['hit'] + $hit;

    $ab = $_POST['ab'][$id];
    $allab = $all['ab'] + $ab;

    $bb = $_POST['bb'][$id];
    $allbb = $all['bb'] + $bb;

    $secondb = $_POST['2b'][$id];
    $all2b = $all['2b'] + $secondb;

    $thirdb = $_POST['3b'][$id];
    $all3b = $all['3b'] + $thirdb;

    $hr = $_POST['hr'][$id];
    $allhr = $all['hr'] + $hr;

    $rbi = $_POST['rbi'][$id];
    $allrbi = $all['rbi'] + $rbi;

    $queryUpdateAll = "
    UPDATE stats_all 
    SET hit = '$allhit' , ab = '$allab', bb = '$allbb', 2b = 'all2b', 3b = 'all3b', hr = '$allhr',  rbi = '$allrbi'
    WHERE playerID = '$player'
    ";
    $updateAll = mysql_query($queryUpdateAll) or die("<p class='style5'>Update query failed: " . mysql_error()) . "</p>";
}
?>

 

This doesn't seem to be working.  ???

 

Something like

 

<?php
foreach ($_POST['playerID'] as $k => $playerID) {
           $hits = $_POST['hits'][$k];
           $ab = $_POST['ab'][$k];
           $bb = $_POST['bb'][$k];
           
           $sql = "UPDATE stats_all SET
                    hits = hits + $hits,
                    ab = ab + $ab,
                    bb = bb + $bb
           WHERE playerID = '$playerID' ";
?>

Wastes time getting the existing values, incrementing them, then updating when you can update them directly.

 

Unless, of course, you need the array for something else, such as display.

 

 

In your update query, $player is your array data, so

 

WHERE playerID = '$player'

 

should be

 

WHERE playerID = '$playerID'

I understand what you've done. Thank you.

 

Can you perform only limited operations within a query? Before, I was calculating an average outside of the query like this:

 

<?php
$decimal = @($hit / $ab); # @ prevents division by zero error
$avg = round($decimal,3); # round to 3 decimal places
?>

 

I'm trying this:

 

SET avg = round( @($hit/$ab),3 )

 

... and of course I get an error.

 

 

 

You know I have to make things more complicated than they really are. The operation works fine outside of the query.

 

<?php
$decimal = @($hit / $ab); # @ prevents division by zero error
$avg = round($decimal,3); # round to 3 decimal places

$queryUpdateAll = "
UPDATE stats_all 
SET hit = hit + '$hit', 
ab = ab + '$ab', 
bb = bb + '$bb', 
2b = 2b + '$secondb', 
3b = 3b + '$thirdb', 
hr = hr + '$hr',  
rbi = rbi + '$rbi',
avg = '$avg'
WHERE playerID = '$playerID'
";
?>

 

This works perfectly fine.  ;) Thanks for all of your help as usual.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.