0

Posted earlier in the wrong forum...update here:

Hey there...my code below had tradtionally only updated 1 of 4 fields. Now I want to update 2 of 8, but when I enter two variables it crashes. Any help would be appreciated. Code is here

    if( isset($_POST['Submit']) && $_POST['Submit']=='Submit' )
{
    foreach($_POST['CFHL_A'] as $playerID=>$v)
    {
        //initialize to empty string and add field=value only if something was submitted
        $update="";

        $CFHL_A=mysql_real_escape_string( trim($_POST['CFHL_A'][$playerID]) );
        $E1314_CFHL_A=mysql_real_escape_string( trim($_POST['E1314_CFHL_A'][$playerID]) ); 
        $CFHL_B=mysql_real_escape_string( trim($_POST['CFHL_B'][$playerID]) );
        $E1314_CFHL_B=mysql_real_escape_string( trim($_POST['E1314_CFHL_B'][$playerID]) );   
        $MFHL=mysql_real_escape_string( trim($_POST['MFHL'][$playerID]) ); 
        $E1314_MFHL=mysql_real_escape_string( trim($_POST['E1314_MFHL'][$playerID]) ); 
        $WFHL=mysql_real_escape_string( trim($_POST['WFHL'][$playerID]) ); 
        $E1314_WFHL=mysql_real_escape_string( trim($_POST['E1314_WFHL'][$playerID]) ); 

        if( !empty($CFHL_A) ) {
            $update.="`CFHL_A`='".$CFHL_A."'";
        }

        if( !empty($E1314_CFHL_A) ) {
            $update.="`E1314_CFHL_A`='".$E1314_CFHL_A."'";
        }

       if( !empty($CFHL_B) ) {
            $update.="`CFHL_B`='".$CFHL_B."'";
        }

       if( !empty($E1314_CFHL_B) ) {
            $update.="`E1314_CFHL_B`='".$E1314_CFHL_B."'";
        }

       if( !empty($MFHL) ) {
            $update.="`MFHL`='".$MFHL."'";
        }

       if( !empty($E1314_MFHL) ) {
            $update.="`E1314_MFHL`='".$E1314_MFHL."'";
        }

       if( !empty($WFHL) ) {
            $update.="`WFHL`='".$WFHL."'";
        }

       if( !empty($E1314_WFHL) ) {
            $update.="`E1314_WFHL`='".$E1314_WFHL."'";
        }


        if(!empty($update))
        {
            $update = 'UPDATE `playerdb` SET '. $update . " WHERE `playerID` ='".$playerID."'";
            mysql_query($update) or die( sprintf('Error @Line %d while trying to execute<br/>%s<br/>%s',__LINE__,$update, mysql_error() ) );
        }
    }

}

// Get all the data from the "player" table
error_reporting (E_ERROR);
4
Contributors
3
Replies
18
Views
4 Years
Discussion Span
Last Post by pritaeas
0

but when I enter two variables it crashes

What do you mean by that? Does your computer crash? Or you just get an error message instead of expected outoput? If there is an error message, please post it. If it isn't enable reporting and display of error messages.

If user submits an empty form (no fields are filled-in) then none of the variables might exist at all. I would do a more thorough check in the if statements:

if(isset($CFHL_A) && !empty($CFHL_A) ) 
{
    ...
}
0

This loop makes the assumption that if if a certain CFHL_A[player_id] exists then so do E1314_CFHL_A[player_id], CFHL_B[player_id] (etc etc).

Could it be the case that these postfields do not exist, in which case a statement like this...

$CFHL_B=mysql_real_escape_string( trim($_POST['CFHL_B'][$playerID]) );

...should throw an error.

Also I can't see commas in your update concatenation (e.g.):

   if( !empty($CFHL_A) ) {
        $update.="`CFHL_A`='".$CFHL_A."'";
    }
   //...
   if( !empty($MFHL) ) {
        $update.="`MFHL`='".$MFHL."'";
    }
   if( !empty($E1314_MFHL) ) {
        $update.="`E1314_MFHL`='".$E1314_MFHL."'";
    }

This looks over-complicated and incomplete, I suggest...

   if( !empty($CFHL_A) ) {
        $update.=",`CFHL_A`='$CFHL_A'";
    }
   //...
   if( !empty($MFHL) ) {
        $update.=",`MFHL`='$MFHL'";
    }
   if( !empty($E1314_MFHL) ) {
        $update.=",`E1314_MFHL`='$E1314_MFHL'";
    }

The missing commas will certainly cause an error. You need to take off the
first character (comma in the $update var)...

if(!empty($update))$update = substr($update,1);

Or something like that :)

0

Using the comma's is tricky, as it can cause a query error. My suggestion would be to use an array, followed by implode:

$update = array ();
if( !empty($CFHL_A) ) {
    $update[] = "`CFHL_A`='$CFHL_A'";
}
//...
if( !empty($MFHL) ) {
    $update[] = "`MFHL`='$MFHL'";
}
if( !empty($E1314_MFHL) ) {
    $update[] = "`E1314_MFHL`='$E1314_MFHL'";
}
$update = implode(', ', $update);
Votes + Comments
agreed
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.