Jump to content

[SOLVED] Loop to C/R/U/D team roster


webguy262

Recommended Posts

PROGRESS!

 

I had to align two other cols to match the dbase.

 

And I changed $player_info['primary_key'] to $player_info['player_id'] (player_id is the auto-incrementing, primary key in the dbase).

 

After those changes, I entered one row in the form and it processed successfully (no errors and the data went into the database).

 

Some questions remain, however:

 

1) Both the single, filled-in row and the 14 blank ones went into the dbase, all with negative 'player_id' numbers.  I thought that any rows that had data were going to go into the database with a positive 'player_id' so they could be distinguished from the blank 'placeholder' rows.  Indeed, when I view the roster the completed column does not appear.

 

2) After the form processes, it should display the roster form.  Right now, it just ends with a single echo of 5a.

 

3) Related to #1: when I try to process the form a second time, I just a string of "Duplicate entry '-1' for key 1" because the negative player_id's are in the dbase.

 

Here is the most recent code:

 

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php



if( !empty( $_POST ) ) {
   /**
   * If you print_r( $_POST ) you will find that you have an array named 'players'.
   * Each index into this array will be POSITIVE and the players database ID if they already exist in the database
   * The index will be NEGATIVE if the player is new and needs to be inserted.
   *
   * And then each player is an array where the associative names should match your column names,
   * so that you can easily generate your insert / update statements based on what I
    * showed you earlier.
   */
   foreach( $_POST['player'] as $player_id => $player_info ) {
     foreach( $player_info as $k => $v ) {
      $player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
      }
      echo "1<br />";
     if( $player_id < 0 ) {
         $player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
       $stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
         . " ) values ( " . implode( ', ', $player_info ) . " )";
          echo "2<br />";
      }else{
      foreach( $player_info as $k => $v ) {
         $player_info[$k] = '`' . $k . '`=' . $v;
          echo "3<br />";
      }
      $stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
         . "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
          echo "4<br />"; 
     }
     $r = mysql_query( $stmt ); // check for errors and success
	if( !$r ) {
  		echo mysql_error() . "<br>";
	}
       echo "5<br />";
    }
   echo "5a<br />";
   exit(); return; // whatever is appropriate to stop processing
    echo "6<br />";
}

echo "<form action=\"roster.php\" method=\"post\"><table>";
   
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="1"', 'height_inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
      $player = mysql_fetch_assoc( $result );
    }
   if( !$player ) {
      // We've run out of players, so create a blank one to insert
        $result = null; // stop trying to access result
      $player = $blankplayer;
        $player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   foreach( $cols as $c => $extra ) {
     $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
     echo "<td>" . tep_draw_input_field( "player[{$player['id']}][{$c}]", $row[$c], $extra ) ."</td>";
    }
    $player = null; // important!
   echo "</tr>";
    
}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

 

Thanks again for helping me through this!

 

 

Link to comment
Share on other sites

  • Replies 59
  • Created
  • Last Reply

Top Posters In This Topic

And I changed $player_info['primary_key'] to $player_info['player_id'] (player_id is the auto-incrementing, primary key in the dbase).

That was the idea all along.  When receiving help on forums or internet chat, unless you're question is really, really simple, you'll rarely get a complete working answer straight away.  Many times we use placeholders for values since we don't know the details of your application.  In this instance I didn't know exactly what you had named your primary key field so I used the word 'primary_key,' it was up to you to figure out that you should replace it with the actual primary key.  Anyways I'm glad you're not just copying and pasting code and then coming back and saying "It doesn't work what now?"  At least your partially trying to process it yourself and find the solution on your own; that's a great start for learning how to program and best of all become a self-sufficient programmer.

 

This should take care of #1 and #3.  If it works I'll help you with your other question.

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
$navigation->set_snapshot();
tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php



if( !empty( $_POST ) ) {
/**
* If you print_r( $_POST ) you will find that you have an array named 'players'.
* Each index into this array will be POSITIVE and the players database ID if they already exist in the database
* The index will be NEGATIVE if the player is new and needs to be inserted.
*
* And then each player is an array where the associative names should match your column names,
* so that you can easily generate your insert / update statements based on what I
* showed you earlier.
*/
foreach( $_POST['player'] as $player_id => $player_info ) {
	foreach( $player_info as $k => $v ) {
		$player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
	}
	// ADDED A CHECK FOR IF THE PLAYER IS EMPTY!
	$emptyplayer = trim( implode( " ", $player_info ) ) == ''; // true if empty, false otherwise
	echo "1<br />";
	if( $player_id < 0 ) {
		if( $emptyplayer ) {
			continue; // SKIP TO NEXT PLAYER
		}
		// OOPS!  SINCE 'player_id' IS AUTO-INCREMENTING, WE DO NOT HAVE TO INSERT IT.
		// THEREFORE THE FOLLOWING LINE IS COMMENTED OUT (i.e. you can remove it from your code).
		//$player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
		$stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
			. " ) values ( " . implode( ', ', $player_info ) . " )";
		echo "2<br />";
	}else if( !$emptyplayer ){
		// player_id GREATER THAN ZERO, SO PLAYER EXISTS IN DATABASE.  PLAYER IS NOT
		// EMPTY, SO WE UPDATE HIM!
		foreach( $player_info as $k => $v ) {
			// SLIGHTLY MORE READABLE
			$player_info[$k] = "`{$k}`={$v}";
			echo "3<br />";
		}
		$stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
			. "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		echo "4<br />";
	}else{
		// player_id GREATER THAN ZERO SO HE EXISTS IN DATABASE.  PLAYER IS EMPTY
		// SO WE DELETE HIM!
		$stmt = "delete from `rosters` where `player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		echo "deletestmt<br />";
	}
	$r = mysql_query( $stmt ); // check for errors and success
	if( !$r ) {
		echo mysql_error() . "<br>";
	}
	echo "5<br />";
}
echo "5a<br />";
exit(); return; // whatever is appropriate to stop processing
echo "6<br />";
}

echo "<form action=\"roster.php\" method=\"post\"><table>";

$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="1"', 'height_inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
  $player = mysql_fetch_assoc( $result );
}
   if( !$player ) {
  // We've run out of players, so create a blank one to insert
	$result = null; // stop trying to access result
  $player = $blankplayer;
	$player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   foreach( $cols as $c => $extra ) {
 $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
 echo "<td>" . tep_draw_input_field( "player[{$player['id']}][{$c}]", $row[$c], $extra ) ."</td>";
}
$player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

Link to comment
Share on other sites

Updated the code and entered one row.  I got that row and 14 blank one with player_id 1-15 (positive #'s).  The echos are 125, etc, 5a.

 

Questions...

 

1) I realize that a key piece of data is not getting entered.  It's the hidden field $customer_id and refers to the coach of the team;  the roster 'belongs' to the coach so each player needs their coach_id in his row.  The coach_id col has always been part of the dbase, but I lost track of it as we were getting all the visible fields to appear and process.  To add this hidden field, I changed this...

 

// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
     $player = mysql_fetch_assoc( $result );
   }

 

... to this...

 

// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr><td><input type=\"hidden\" name=\"customer_id\" value=\"$customer_id\"></td>";
   if( $result ) {
     $player = mysql_fetch_assoc( $result );
   }

 

It displays the html correctly, but I need to figure out how to include this field in the insert/update statements.  Do I just add it to the $cols array?  It does not have a "size" attribute so I'm thinking that would make it not fit the pattern.  Could I say 'customer_id' => 'type="hidden"' ?  I suspect, however, that I might need to look at the tep_draw_input_field function as there is one in the application:

 

// Output a form hidden field
  function tep_draw_hidden_field($name, $value = '', $parameters = '') {
    $field = '<input type="hidden" name="' . tep_output_string($name) . '"';

    if (tep_not_null($value)) {
      $field .= ' value="' . tep_output_string($value) . '"';
    } elseif (isset($GLOBALS[$name])) {
      $field .= ' value="' . tep_output_string(stripslashes($GLOBALS[$name])) . '"';
    }

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    return $field;
  }

////

 

Instead of calling that function, can the customer_id var/value pair be included by 'hard coding' them in the insert/updates after the cols are imploded?

 

"insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
            . ", customer_id ) values ( " . implode( ', ', $player_info ) . ", $customer_id )";

 

 

Am I even close?

 

2) When I view the roster when there is a row in the database I do not see the row.  That's how I discovered that there was no customer_id value going in.  Customer_id will serve as the WHERE in the SELECT when loading a roster.  So I know I need to hook all that up.

 

Anyway, when I entered data in the form a second time -- with 15 rows in the dbase, row one complete and all others empty -- the form created an additional 15 rows (this time I left the first row blank and entered the data in the second row; it ended up in row 17 which makes sense).  So I still need a way to only ever have 15 rows for any coach.  And that probably brings me back to entering and accounting for the customer_id field, right?

 

Dude, you have been a great resource on this.  Best help I have ever gotten from a board.  And given my programming skills(!) you know that's saying something!

 

Thanks again!

 

 

Link to comment
Share on other sites

For #1:

echo "<tr><td><input type=\"hidden\" name=\"player[{$i}][customer_id]\" value=\"{$customer_id}\"></td>";

 

Notice the name attribute?  That will turn it into: $_POST['player_id']['customer_id'], just like the other fields.  Then it will just work with the existing update and insert statements.

 

I have to run so I'll check on what #2 was later...

Link to comment
Share on other sites

I'd like to figure out why the blank rows are going in.  At some point do me a favor and perform this experiment.

 

1) Delete all of the data from the table.

 

2) Add this modify some code for me:

if( !empty( $_POST ) ) {

 

to:

echo 'POST OUTPUT:<br /><pre style="text-align: left;">';
var_dump( $_POST );
echo '</pre>';
if( !empty( $_POST ) ) {

 

3) Resubmit the form, filling in only a couple of the rows.

 

4) On the resulting page, copy and paste the output from the code I had you add in step two.

Link to comment
Share on other sites

Thanks as always!

 

Here's what resulted...

 

(BTW, the $customer_id is correct in the array below but did not go into the dbase.)

 


POST OUTPUT:

array(2) {
  ["player"]=>
  array(30) {
    [1]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-1"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(1) "e"
      ["player_roster_lname"]=>
      string(1) "e"
      ["player_roster_address"]=>
      string(1) "e"
      ["player_roster_city"]=>
      string(1) "e"
      ["player_roster_state"]=>
      string(1) "e"
      ["player_roster_zip"]=>
      string(1) "e"
      ["player_roster_phone"]=>
      string(1) "e"
      ["player_roster_email"]=>
      string(1) "e"
      ["player_roster_number"]=>
      string(1) "e"
      ["player_roster_gradyear"]=>
      string(1) "e"
      ["player_roster_height_feet"]=>
      string(1) "e"
      ["player_roster_height_inches"]=>
      string(1) "e"
    }
    [2]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-2"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(1) "f"
      ["player_roster_lname"]=>
      string(1) "f"
      ["player_roster_address"]=>
      string(1) "f"
      ["player_roster_city"]=>
      string(1) "f"
      ["player_roster_state"]=>
      string(1) "f"
      ["player_roster_zip"]=>
      string(1) "f"
      ["player_roster_phone"]=>
      string(1) "f"
      ["player_roster_email"]=>
      string(1) "f"
      ["player_roster_number"]=>
      string(1) "f"
      ["player_roster_gradyear"]=>
      string(1) "f"
      ["player_roster_height_feet"]=>
      string(1) "f"
      ["player_roster_height_inches"]=>
      string(1) "f"
    }
    [3]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-3"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(1) "g"
      ["player_roster_lname"]=>
      string(1) "g"
      ["player_roster_address"]=>
      string(1) "g"
      ["player_roster_city"]=>
      string(1) "g"
      ["player_roster_state"]=>
      string(1) "g"
      ["player_roster_zip"]=>
      string(1) "g"
      ["player_roster_phone"]=>
      string(1) "g"
      ["player_roster_email"]=>
      string(1) "g"
      ["player_roster_number"]=>
      string(1) "g"
      ["player_roster_gradyear"]=>
      string(1) "g"
      ["player_roster_height_feet"]=>
      string(1) "g"
      ["player_roster_height_inches"]=>
      string(1) "g"
    }
    [4]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-4"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [5]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-5"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [6]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-6"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [7]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-7"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [8]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-8"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [9]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-9"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [10]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-10"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [11]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-11"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [12]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-12"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [13]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-13"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [14]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-14"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
    [15]=>
    array(1) {
      ["customer_id"]=>
      string(4) "1112"
    }
    ["-15"]=>
    array(12) {
      ["player_roster_fname"]=>
      string(0) ""
      ["player_roster_lname"]=>
      string(0) ""
      ["player_roster_address"]=>
      string(0) ""
      ["player_roster_city"]=>
      string(0) ""
      ["player_roster_state"]=>
      string(0) ""
      ["player_roster_zip"]=>
      string(0) ""
      ["player_roster_phone"]=>
      string(0) ""
      ["player_roster_email"]=>
      string(0) ""
      ["player_roster_number"]=>
      string(0) ""
      ["player_roster_gradyear"]=>
      string(0) ""
      ["player_roster_height_feet"]=>
      string(0) ""
      ["player_roster_height_inches"]=>
      string(0) ""
    }
  }
  ["editplayers"]=>
  string(6) "Submit"
}

1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
1
3
4
5
1
2
5
5a

Link to comment
Share on other sites

Run this and post the output from the DEBUG dump.

 

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
$navigation->set_snapshot();
tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php


mydbg( 'POST: ' ); mydbg( $_POST );//%%
if( !empty( $_POST ) ) {
/**
* If you print_r( $_POST ) you will find that you have an array named 'players'.
* Each index into this array will be POSITIVE and the players database ID if they already exist in the database
* The index will be NEGATIVE if the player is new and needs to be inserted.
*
* And then each player is an array where the associative names should match your column names,
* so that you can easily generate your insert / update statements based on what I
* showed you earlier.
*/
foreach( $_POST['player'] as $player_id => $player_info ) {
	foreach( $player_info as $k => $v ) {
		$player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
	}
	// ADDED A CHECK FOR IF THE PLAYER IS EMPTY!
	$emptyplayer = trim( implode( " ", $player_info ) ) == ''; // true if empty, false otherwise
	echo "1<br />";
	if( $player_id < 0 ) {
		if( $emptyplayer ) {
			continue; // SKIP TO NEXT PLAYER
		}
		// OOPS!  SINCE 'player_id' IS AUTO-INCREMENTING, WE DO NOT HAVE TO INSERT IT.
		// THEREFORE THE FOLLOWING LINE IS COMMENTED OUT (i.e. you can remove it from your code).
		//$player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
		$stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
			. " ) values ( " . implode( ', ', $player_info ) . " )";
		echo "2<br />";
	}else if( !$emptyplayer ){
		// player_id GREATER THAN ZERO, SO PLAYER EXISTS IN DATABASE.  PLAYER IS NOT
		// EMPTY, SO WE UPDATE HIM!
		foreach( $player_info as $k => $v ) {
			// SLIGHTLY MORE READABLE
			$player_info[$k] = "`{$k}`={$v}";
			echo "3<br />";
		}
		$stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
			. "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		echo "4<br />";
	}else{
		// player_id GREATER THAN ZERO SO HE EXISTS IN DATABASE.  PLAYER IS EMPTY
		// SO WE DELETE HIM!
		$stmt = "delete from `rosters` where `player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		echo "deletestmt<br />";
	}
	$r = mysql_query( $stmt ); // check for errors and success
	if( !$r ) {
		echo mysql_error() . "<br>";
	}
	echo "5<br />";
}
echo "5a<br />";
echo "6<br />";
}

echo "<form action=\"roster.php\" method=\"post\"><table>";

mydbg( $sqlplayers );//%%
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="1"', 'height_inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
  $player = mysql_fetch_assoc( $result );
  mydbg( $player );//%%
}
   if( !$player ) {
  // We've run out of players, so create a blank one to insert
	$result = null; // stop trying to access result
  $player = $blankplayer;
	$player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   $firstcol = true;
   foreach( $cols as $c => $extra ) {
 $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
 echo "<td>";
 if( $firstcol === true ) {
	echo "<input type=\"hidden\" name=\"player[{$player['id']}][customer_id]\" value=\"{$customer_id}\" />";
 }
 echo tep_draw_input_field( "player[{$player['id']}][{$c}]", $row[$c], $extra ) ."</td>";
}
$player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<?php mydbg( null, false ); /*%%REMOVE ME dump debugging */?>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php
/**
* Simple debugging function.  If $add is true, it adds debugging message.  If $add is
* is false, it dumps debugging messages that were added.
*
* @param mixed $msg
* @param bool $add
*/
function mydbg( $msg, $add = true ) {
static $msgs = array();
if( $add === true ) {
	$msgs[] = $msg;
}else{
	echo '<pre style="text-align: left; font-weight: bold; font-size: 10px; background-color: #ececec;">
	DEBUG:';
	foreach( $msgs as $msg ) {
		if( is_bool( $msg ) ) {
			$msg = 'BOOLEAN [' . ($msg ? 'T' : 'F' ) . ']';
		}else if( is_null( $msg ) ) {
			$msg = '__NULL__';
		}else if( is_string( $msg ) && !strlen( $msg ) ) {
			$msg = '__EMPTY_STRING__';
		}else if( is_array( $msg ) || is_object( $msg ) ) {
			$msg = print_r( $msg, true );
		}
		echo $msg . "\n\n";
	}
	echo '</pre>';
}
}

Link to comment
Share on other sites

Here it is...

 

     DEBUG:POST: 

Array
(
    [player] => Array
        (
            [-1] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => w
                    [player_roster_lname] => w
                    [player_roster_address] => w
                    [player_roster_city] => w
                    [player_roster_state] => w
                    [player_roster_zip] => w
                    [player_roster_phone] => w
                    [player_roster_email] => w
                    [player_roster_number] => ww
                    [player_roster_gradyear] => w
                    [player_roster_height_feet] => w
                    [player_roster_height_inches] => w
                )

            [-2] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => d
                    [player_roster_lname] => d
                    [player_roster_address] => dd
                    [player_roster_city] => d
                    [player_roster_state] => d
                    [player_roster_zip] => dd
                    [player_roster_phone] => d
                    [player_roster_email] => d
                    [player_roster_number] => dd
                    [player_roster_gradyear] => dd
                    [player_roster_height_feet] => d
                    [player_roster_height_inches] => d
                )

            [-3] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => s
                    [player_roster_lname] => s
                    [player_roster_address] => s
                    [player_roster_city] => s
                    [player_roster_state] => s
                    [player_roster_zip] => s
                    [player_roster_phone] => s
                    [player_roster_email] => s
                    [player_roster_number] => s
                    [player_roster_gradyear] => s
                    [player_roster_height_feet] => s
                    [player_roster_height_inches] => s
                )

            [-4] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-5] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-6] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-7] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-8] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-9] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-10] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-11] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-12] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-13] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-14] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-15] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

        )

    [editplayers] => Submit
)


__NULL__

Link to comment
Share on other sites

Empty your table again.  I'd like to see the debug output of this script for the following three scenarios:

 

1) The initial load when the table is empty.

 

2) The output after submitting the form.

 

3) The output when the page is requested AND players exist in the database.

 

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
$navigation->set_snapshot();
tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php


mydbg( 'POST: ' ); mydbg( $_POST );//%%
if( !empty( $_POST ) ) {
/**
* If you print_r( $_POST ) you will find that you have an array named 'players'.
* Each index into this array will be POSITIVE and the players database ID if they already exist in the database
* The index will be NEGATIVE if the player is new and needs to be inserted.
*
* And then each player is an array where the associative names should match your column names,
* so that you can easily generate your insert / update statements based on what I
* showed you earlier.
*/
foreach( $_POST['player'] as $player_id => $player_info ) {
	foreach( $player_info as $k => $v ) {
		$player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
	}
	// ADDED A CHECK FOR IF THE PLAYER IS EMPTY!
	$emptyplayer = $player_info; // Copy the array
	unset( $emptyplayer['customer_id'] ); // Remove customer_id from empty player
	$emptyplayer = !strlen( trim( implode( " ", $emptyplayer ) ) ); // true if empty, false otherwise
	mydbg( 'Empty Player:' );mydbg( $emptyplayer );//%%
	if( $player_id < 0 ) {
		if( $emptyplayer ) {
			mydbg( 'Skipped player' );//%%
			continue; // SKIP TO NEXT PLAYER
		}
		// OOPS!  SINCE 'player_id' IS AUTO-INCREMENTING, WE DO NOT HAVE TO INSERT IT.
		// THEREFORE THE FOLLOWING LINE IS COMMENTED OUT (i.e. you can remove it from your code).
		//$player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
		$stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
			. " ) values ( " . implode( ', ', $player_info ) . " )";
		mydbg( 'Insert player' );//%%
	}else if( !$emptyplayer ){
		// player_id GREATER THAN ZERO, SO PLAYER EXISTS IN DATABASE.  PLAYER IS NOT
		// EMPTY, SO WE UPDATE HIM!
		foreach( $player_info as $k => $v ) {
			// SLIGHTLY MORE READABLE
			$player_info[$k] = "`{$k}`={$v}";
		}
		$stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
			. "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		mydbg( 'Update player' );//%%
	}else{
		// player_id GREATER THAN ZERO SO HE EXISTS IN DATABASE.  PLAYER IS EMPTY
		// SO WE DELETE HIM!
		$stmt = "delete from `rosters` where `player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		mydbg( 'Delete player' );//%%
	}
	$r = mysql_query( $stmt ); // check for errors and success
	if( !$r ) {
		mydbg( $stmt );//%%
		mydbg( mysql_error() );//%%
	}
}
}

echo "<form action=\"roster.php\" method=\"post\"><table>";

mydbg( 'Select players:' ); mydbg( $sqlplayers );//%%
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="1"', 'height_inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
  $player = mysql_fetch_assoc( $result );
  mydbg( $player );//%%
}
   if( !$player ) {
  // We've run out of players, so create a blank one to insert
	$result = null; // stop trying to access result
  $player = $blankplayer;
	$player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   $firstcol = true;
   foreach( $cols as $c => $extra ) {
 $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
 echo "<td>";
 if( $firstcol === true ) {
	echo "<input type=\"hidden\" name=\"player[{$player['id']}][customer_id]\" value=\"{$customer_id}\" />";
 }
 echo tep_draw_input_field( "player[{$player['id']}][{$c}]", $row[$c], $extra ) ."</td>";
}
$player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<?php mydbg( null, false ); /*%%REMOVE ME dump debugging */?>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php
/**
* Simple debugging function.  If $add is true, it adds debugging message.  If $add is
* is false, it dumps debugging messages that were added.
*
* @param mixed $msg
* @param bool $add
*/
function mydbg( $msg, $add = true ) {
static $msgs = array();
if( $add === true ) {
	$msgs[] = $msg;
}else{
	echo '<pre style="text-align: left; font-weight: bold; font-size: 10px; background-color: #ececec;">
	DEBUG:';
	foreach( $msgs as $msg ) {
		if( is_bool( $msg ) ) {
			$msg = 'BOOLEAN [' . ($msg ? 'T' : 'F' ) . ']';
		}else if( is_null( $msg ) ) {
			$msg = '__NULL__';
		}else if( is_string( $msg ) && !strlen( $msg ) ) {
			$msg = '__EMPTY_STRING__';
		}else if( is_array( $msg ) || is_object( $msg ) ) {
			$msg = print_r( $msg, true );
		}
		echo $msg . "\n\n";
	}
	echo '</pre>';
}
}

Link to comment
Share on other sites

Empty dbase & empty table

 

DEBUG:POST: 

Array
(
)


Select players:

__NULL__

 

After entering 3 rows and submitting form...

 

      DEBUG:POST: 

Array
(
    [player] => Array
        (
            [-1] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => w
                    [player_roster_lname] => w
                    [player_roster_address] => w
                    [player_roster_city] => w
                    [player_roster_state] => w
                    [player_roster_zip] => w
                    [player_roster_phone] => w
                    [player_roster_email] => w
                    [player_roster_number] => w
                    [player_roster_gradyear] => w
                    [player_roster_height_feet] => w
                    [player_roster_height_inches] => w
                )

            [-2] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => q
                    [player_roster_lname] => q
                    [player_roster_address] => q
                    [player_roster_city] => q
                    [player_roster_state] => q
                    [player_roster_zip] => q
                    [player_roster_phone] => q
                    [player_roster_email] => q
                    [player_roster_number] => q
                    [player_roster_gradyear] => q
                    [player_roster_height_feet] => q
                    [player_roster_height_inches] => q
                )

            [-3] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => e
                    [player_roster_lname] => e
                    [player_roster_address] => e
                    [player_roster_city] => e
                    [player_roster_state] => e
                    [player_roster_zip] => e
                    [player_roster_phone] => e
                    [player_roster_email] => e
                    [player_roster_number] => e
                    [player_roster_gradyear] => e
                    [player_roster_height_feet] => e
                    [player_roster_height_inches] => e
                )

            [-4] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-5] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-6] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-7] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-8] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-9] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-10] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-11] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-12] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-13] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-14] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-15] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

        )

    [editplayers] => Submit
)


Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

BOOLEAN [F]

Insert player

Select players:

__NULL__

 

You did not ask for this, but here is what ends up in the dbase (row headings and data)

 

player_id

customer_id

roster_date

player_roster_fname

player_roster_lname

player_roster_address

player_roster_city

player_roster_state

player_roster_zip

player_roster_phone

player_roster_email

player_roster_number

player_roster_gradyear

player_roster_height_feet

player_roster_height_inches
Edit 	Delete 	1 	1112 	20090604163049 	w 	w 	w 	w 	w 	w 	w 	w 	w 	w 	w 	w
Edit 	Delete 	2 	1112 	20090604163049 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q
Edit 	Delete 	3 	1112 	20090604163049 	e 	e 	e 	e 	e 	e 	e 	e 	e 	e 	e 	e
Edit 	Delete 	4 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	5 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	6 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	7 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	8 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	9 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	10 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	11 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	12 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	13 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	14 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	  	  	 
Edit 	Delete 	15 	1112 	20090604163049 	  	  	  	  	  	  	  	  	  	 

 

 

Output when the page is requested AND players exist in the database (no rows filled in even tho there are 3 in the dbase):

 

      DEBUG:POST: 

Array
(
)


Select players:

__NULL__

 

BTW, does the fact that we are entering a date and customer # for every row have anything to do with the 'empty' rows going in?  Does it mean, for example, that the row is not being considered empty if it has a customer_id and date value in it?

 

Continued thanks!

 

 

 

Link to comment
Share on other sites

The roster_date column is a timestamp.  Sorry if that has screwed up troubleshooting.

 

It's in there so we can know when a row has changed so we can update records in FileMaker.  Is the timestamp causing a problem?  If so, the date can come in as a post variable can't it?

Link to comment
Share on other sites

I am using the exact code you send me.  At some point we started trading the entire file... all the code on the entire page.  Ever since then, I have just copied and pasted your latest code and uploaded it.

 

The timestamp is not in the code anywhere, it is in the database structure as the datatype.

 

Here is the sql...

 

-- 
-- Table structure for table `rosters`
-- 

CREATE TABLE `rosters` (
  `player_id` tinyint(4) NOT NULL auto_increment,
  `customer_id` int(11) NOT NULL default '0',
  `roster_date` timestamp(14) NOT NULL,
  `player_roster_fname` varchar(32) NOT NULL default '',
  `player_roster_lname` varchar(32) NOT NULL default '',
  `player_roster_address` varchar(32) NOT NULL default '',
  `player_roster_city` varchar(32) NOT NULL default '',
  `player_roster_state` varchar(32) NOT NULL default '',
  `player_roster_zip` varchar(32) NOT NULL default '',
  `player_roster_phone` varchar(32) NOT NULL default '',
  `player_roster_email` varchar(32) NOT NULL default '',
  `player_roster_number` varchar(32) NOT NULL default '',
  `player_roster_gradyear` varchar(32) NOT NULL default '',
  `player_roster_height_feet` varchar(32) NOT NULL default '',
  `player_roster_height_inches` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`player_id`),
  KEY `customers_id` (`customer_id`)
) TYPE=MyISAM AUTO_INCREMENT=16 ;

Link to comment
Share on other sites

Bah!  I have brain damage that's what the problem is.  I put the empty player check in the wrong place.  The following code should fix it.  I've also made an attempt at fixing why your players are not appearing in the form after they're entered into the database.

 

Try:

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
$navigation->set_snapshot();
tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php


mydbg( 'POST: ' ); mydbg( $_POST );//%%
if( !empty( $_POST ) ) {
/**
* If you print_r( $_POST ) you will find that you have an array named 'players'.
* Each index into this array will be POSITIVE and the players database ID if they already exist in the database
* The index will be NEGATIVE if the player is new and needs to be inserted.
*
* And then each player is an array where the associative names should match your column names,
* so that you can easily generate your insert / update statements based on what I
* showed you earlier.
*/
foreach( $_POST['player'] as $player_id => $player_info ) {
	// ADDED A CHECK FOR IF THE PLAYER IS EMPTY!
	$emptyplayer = $player_info; // Copy the array
	unset( $emptyplayer['customer_id'] ); // Remove customer_id from empty player
	$emptyplayer = trim( implode( " ", $emptyplayer ) );
	mydbg( 'Empty Player:' );mydbg( $emptyplayer );//%%
	$emptyplayer = !strlen( $emptyplayer ); // true if empty, false otherwise
	mydbg( 'Empty Player:' );mydbg( $emptyplayer );//%%
	foreach( $player_info as $k => $v ) {
		$player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
	}
	if( $player_id < 0 ) {
		if( $emptyplayer ) {
			mydbg( 'Skipped player' );//%%
			continue; // SKIP TO NEXT PLAYER
		}
		// OOPS!  SINCE 'player_id' IS AUTO-INCREMENTING, WE DO NOT HAVE TO INSERT IT.
		// THEREFORE THE FOLLOWING LINE IS COMMENTED OUT (i.e. you can remove it from your code).
		//$player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
		$stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
			. " ) values ( " . implode( ', ', $player_info ) . " )";
		mydbg( 'Insert player' );//%%
	}else if( !$emptyplayer ){
		// player_id GREATER THAN ZERO, SO PLAYER EXISTS IN DATABASE.  PLAYER IS NOT
		// EMPTY, SO WE UPDATE HIM!
		foreach( $player_info as $k => $v ) {
			// SLIGHTLY MORE READABLE
			$player_info[$k] = "`{$k}`={$v}";
		}
		$stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
			. "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		mydbg( 'Update player' );//%%
	}else{
		// player_id GREATER THAN ZERO SO HE EXISTS IN DATABASE.  PLAYER IS EMPTY
		// SO WE DELETE HIM!
		$stmt = "delete from `rosters` where `player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		mydbg( 'Delete player' );//%%
	}
	$r = mysql_query( $stmt ); // check for errors and success
	if( !$r ) {
		mydbg( $stmt );//%%
		mydbg( mysql_error() );//%%
	}
}
}

echo "<form action=\"roster.php\" method=\"post\"><table>";

mydbg( 'Select players:' ); mydbg( $sqlplayers );//%%
// The reason your players are not selected from the database and displayed in the form
// is because $sqlplayers is NULL; you are not running any query.
$sqlplayers = "select * from `rosters` where `customer_id`='" . mysql_real_escape_string( $customer_id ) . "'";
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="1"', 'height_inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
  $player = mysql_fetch_assoc( $result );
  mydbg( $player );//%%
}
   if( !$player ) {
  // We've run out of players, so create a blank one to insert
	$result = null; // stop trying to access result
  $player = $blankplayer;
	$player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   $firstcol = true;
   foreach( $cols as $c => $extra ) {
 $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
 echo "<td>";
 if( $firstcol === true ) {
	echo "<input type=\"hidden\" name=\"player[{$player['id']}][customer_id]\" value=\"{$customer_id}\" />";
 }
 echo tep_draw_input_field( "player[{$player['id']}][{$c}]", $row[$c], $extra ) ."</td>";
}
$player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<?php mydbg( null, false ); /*%%REMOVE ME dump debugging */?>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php
/**
* Simple debugging function.  If $add is true, it adds debugging message.  If $add is
* is false, it dumps debugging messages that were added.
*
* @param mixed $msg
* @param bool $add
*/
function mydbg( $msg, $add = true ) {
static $msgs = array();
if( $add === true ) {
	$msgs[] = $msg;
}else{
	echo '<pre style="text-align: left; font-weight: bold; font-size: 10px; background-color: #ececec;">
	DEBUG:';
	foreach( $msgs as $msg ) {
		if( is_bool( $msg ) ) {
			$msg = 'BOOLEAN [' . ($msg ? 'T' : 'F' ) . ']';
		}else if( is_null( $msg ) ) {
			$msg = '__NULL__';
		}else if( is_string( $msg ) && !strlen( $msg ) ) {
			$msg = '__EMPTY_STRING__';
		}else if( is_array( $msg ) || is_object( $msg ) ) {
			$msg = print_r( $msg, true );
		}
		echo $msg . "\n\n";
	}
	echo '</pre>';
}
}

 

If it produces any problems, then repeat this procedure:

 

Empty your table again.  I'd like to see the debug output of this script for the following three scenarios:

 

1) The initial load when the table is empty.

 

2) The output after submitting the form.

 

3) The output when the page is requested AND players exist in the database.

Link to comment
Share on other sites

PROGRESS!

 

Filled out two rows and (only) two rows went into the dbase, with all fields correct.

 

After the submit, the rows are still not appearing in the form, but they are in the debug output so I hope that's an easy fix.

 

So I went ahead and tried to submit data when there was some already in the form.  That yields some strange behavior described at the end here, along with the debug output.

 

I can't thank you enough for hangin' in with me! 

 

 

Empty table... initial page load...

 

DEBUG:POST: 

Array
(
)


Select players:

__NULL__

BOOLEAN [F]

 

After entering 2 rows...

 

 

      DEBUG:POST: 

Array
(
    [player] => Array
        (
            [-1] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => a
                    [player_roster_lname] => a
                    [player_roster_address] => a
                    [player_roster_city] => a
                    [player_roster_state] => a
                    [player_roster_zip] => a
                    [player_roster_phone] => a
                    [player_roster_email] => a
                    [player_roster_number] => a
                    [player_roster_gradyear] => a
                    [player_roster_height_feet] => a
                    [player_roster_height_inches] => a
                )

            [-2] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => b
                    [player_roster_lname] => b
                    [player_roster_address] => b
                    [player_roster_city] => b
                    [player_roster_state] => b
                    [player_roster_zip] => b
                    [player_roster_phone] => b
                    [player_roster_email] => b
                    [player_roster_number] => b
                    [player_roster_gradyear] => b
                    [player_roster_height_feet] => b
                    [player_roster_height_inches] => b
                )

            [-3] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

...and so on...

            [-15] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

        )

    [editplayers] => Submit
)


Empty Player:

a a a a a a a a a a a a

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

b b b b b b b b b b b b

Empty Player:

BOOLEAN [F]

Insert player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

...etc

Select players:

__NULL__

Array
(
    [player_id] => 1
    [customer_id] => 1112
    [roster_date] => 20090605152854
    [player_roster_fname] => a
    [player_roster_lname] => a
    [player_roster_address] => a
    [player_roster_city] => a
    [player_roster_state] => a
    [player_roster_zip] => a
    [player_roster_phone] => a
    [player_roster_email] => a
    [player_roster_number] => a
    [player_roster_gradyear] => a
    [player_roster_height_feet] => a
    [player_roster_height_inches] => a
)


Array
(
    [player_id] => 2
    [customer_id] => 1112
    [roster_date] => 20090605152854
    [player_roster_fname] => b
    [player_roster_lname] => b
    [player_roster_address] => b
    [player_roster_city] => b
    [player_roster_state] => b
    [player_roster_zip] => b
    [player_roster_phone] => b
    [player_roster_email] => b
    [player_roster_number] => b
    [player_roster_gradyear] => b
    [player_roster_height_feet] => b
    [player_roster_height_inches] => b
)


BOOLEAN [F]

 

Page after reloading it when players are in the dbase, the rows appear in the debug output but not in the form...

 

      DEBUG:POST: 

Array
(
)


Select players:

__NULL__

Array
(
    [player_id] => 1
    [customer_id] => 1112
    [roster_date] => 20090605154115
    [player_roster_fname] => 1
    [player_roster_lname] => 1
    [player_roster_address] => 1
    [player_roster_city] => 1
    [player_roster_state] => 1
    [player_roster_zip] => 1
    [player_roster_phone] => 1
    [player_roster_email] => 1
    [player_roster_number] => 1
    [player_roster_gradyear] => 1
    [player_roster_height_feet] => 1
    [player_roster_height_inches] => 1
)


Array
(
    [player_id] => 2
    [customer_id] => 1112
    [roster_date] => 20090605154115
    [player_roster_fname] => 2
    [player_roster_lname] => 2
    [player_roster_address] => 2
    [player_roster_city] => 2
    [player_roster_state] => 2
    [player_roster_zip] => 2
    [player_roster_phone] => 2
    [player_roster_email] => 2
    [player_roster_number] => 2
    [player_roster_gradyear] => 2
    [player_roster_height_feet] => 2
    [player_roster_height_inches] => 2
)


BOOLEAN [F]

 

I'm also including the output when there are players in the dbase, but none appear in the form, and I enter new values in the first two rows.  This amounts to the 'editing' of an existing roster. 

 

What happened is this... The new data did not go in, the second row of existing data was deleted... but I noticed that the 'fname' field gets updated.

 

I know this output may be meaningless since the players in the dbase are not populating the form.  But I thought I would throw it in.

 

Here's the output:

 

      DEBUG:POST: 

Array
(
    [player] => Array
        (
            [0] => Array
                (
                    [customer_id] => 1112
                )

            [1] => Array
                (
                    [player_roster_fname] => b
                )

            [2] => Array
                (
                    [customer_id] => 1112
                )

            [3] => Array
                (
                    [player_roster_lname] => b
                )

            [4] => Array
                (
                    [customer_id] => 1112
                )

            [5] => Array
                (
                    [player_roster_address] => b
                )

            [6] => Array
                (
                    [customer_id] => 1112
                )

            [7] => Array
                (
                    [player_roster_city] => b
                )

            [8] => Array
                (
                    [customer_id] => 1112
                )

            [9] => Array
                (
                    [player_roster_state] => b
                )

            [10] => Array
                (
                    [customer_id] => 1112
                )

            [11] => Array
                (
                    [player_roster_zip] => b
                )

            [12] => Array
                (
                    [customer_id] => 1112
                )

            [13] => Array
                (
                    [player_roster_phone] => b
                )

            [14] => Array
                (
                    [customer_id] => 1112
                )

            [15] => Array
                (
                    [player_roster_email] => b
                )

            [16] => Array
                (
                    [customer_id] => 1112
                )

            [17] => Array
                (
                    [player_roster_number] => b
                )

            [18] => Array
                (
                    [customer_id] => 1112
                )

            [19] => Array
                (
                    [player_roster_gradyear] => b
                )

            [20] => Array
                (
                    [customer_id] => 1112
                )

            [21] => Array
                (
                    [player_roster_height_feet] => b
                )

            [22] => Array
                (
                    [customer_id] => 1112
                )

            [23] => Array
                (
                    [player_roster_height_inches] => b
                )

            [-1] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-2] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-3] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

...etc. UP TO 14 ONLY (I ASSUME BECAUSE WE HAVE ONE FILLED ROW)

            [-14] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

        )

    [editplayers] => Submit
)


Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Delete player

Empty Player:

b

Empty Player:

BOOLEAN [F]

Update player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Empty Player:

__EMPTY_STRING__

Empty Player:

BOOLEAN [T]

Skipped player

Select players:

__NULL__

Array
(
    [player_id] => 1
    [customer_id] => 1112
    [roster_date] => 20090605155025
    [player_roster_fname] => b  +++++++++++++++++++++++++++++++
    [player_roster_lname] => 1
    [player_roster_address] => 1
    [player_roster_city] => 1
    [player_roster_state] => 1
    [player_roster_zip] => 1
    [player_roster_phone] => 1
    [player_roster_email] => 1
    [player_roster_number] => 1
    [player_roster_gradyear] => 1
    [player_roster_height_feet] => 1
    [player_roster_height_inches] => 1
)


BOOLEAN [F]

 

 

 

Link to comment
Share on other sites

Empty your tables and try your experiments again.  I changed line 112 from $player['id'] to $player['player_id'], which I believe should fix it.

 

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
$navigation->set_snapshot();
tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php


mydbg( 'POST: ' ); mydbg( $_POST );//%%
if( !empty( $_POST ) ) {
/**
* If you print_r( $_POST ) you will find that you have an array named 'players'.
* Each index into this array will be POSITIVE and the players database ID if they already exist in the database
* The index will be NEGATIVE if the player is new and needs to be inserted.
*
* And then each player is an array where the associative names should match your column names,
* so that you can easily generate your insert / update statements based on what I
* showed you earlier.
*/
foreach( $_POST['player'] as $player_id => $player_info ) {
	// ADDED A CHECK FOR IF THE PLAYER IS EMPTY!
	$emptyplayer = $player_info; // Copy the array
	unset( $emptyplayer['customer_id'] ); // Remove customer_id from empty player
	$emptyplayer = trim( implode( " ", $emptyplayer ) );
	$emptyplayer = !strlen( $emptyplayer ); // true if empty, false otherwise
	foreach( $player_info as $k => $v ) {
		$player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
	}
	if( $player_id < 0 ) {
		if( $emptyplayer ) {
			mydbg( 'Skipped player' );//%%
			continue; // SKIP TO NEXT PLAYER
		}
		// OOPS!  SINCE 'player_id' IS AUTO-INCREMENTING, WE DO NOT HAVE TO INSERT IT.
		// THEREFORE THE FOLLOWING LINE IS COMMENTED OUT (i.e. you can remove it from your code).
		//$player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
		$stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
			. " ) values ( " . implode( ', ', $player_info ) . " )";
		mydbg( 'Insert player' );//%%
	}else if( !$emptyplayer ){
		// player_id GREATER THAN ZERO, SO PLAYER EXISTS IN DATABASE.  PLAYER IS NOT
		// EMPTY, SO WE UPDATE HIM!
		foreach( $player_info as $k => $v ) {
			// SLIGHTLY MORE READABLE
			$player_info[$k] = "`{$k}`={$v}";
		}
		$stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
			. "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		mydbg( 'Update player' );//%%
	}else{
		// player_id GREATER THAN ZERO SO HE EXISTS IN DATABASE.  PLAYER IS EMPTY
		// SO WE DELETE HIM!
		$stmt = "delete from `rosters` where `player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		mydbg( 'Delete player' );//%%
	}
	$r = mysql_query( $stmt ); // check for errors and success
	if( !$r ) {
		mydbg( $stmt );//%%
		mydbg( mysql_error() );//%%
	}
}
}

echo "<form action=\"roster.php\" method=\"post\"><table>";

// The reason your players are not selected from the database and displayed in the form
// is because $sqlplayers is NULL; you are not running any query.
$sqlplayers = "select * from `rosters` where `customer_id`='" . mysql_real_escape_string( $customer_id ) . "'";
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="1"', 'height_inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
  $player = mysql_fetch_assoc( $result );
  mydbg( $player );//%%
}
   if( !$player ) {
  // We've run out of players, so create a blank one to insert
	$result = null; // stop trying to access result
  $player = $blankplayer;
	$player['player_id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   $firstcol = true;
   foreach( $cols as $c => $extra ) {
 $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
 echo "<td>";
 if( $firstcol === true ) {
	echo "<input type=\"hidden\" name=\"player[{$player['player_id']}][customer_id]\" value=\"{$customer_id}\" />";
 }
 echo tep_draw_input_field( "player[{$player['player_id']}][{$c}]", $row[$c], $extra ) ."</td>";
}
$player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<?php mydbg( null, false ); /*%%REMOVE ME dump debugging */?>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php
/**
* Simple debugging function.  If $add is true, it adds debugging message.  If $add is
* is false, it dumps debugging messages that were added.
*
* @param mixed $msg
* @param bool $add
*/
function mydbg( $msg, $add = true ) {
static $msgs = array();
if( $add === true ) {
	$msgs[] = $msg;
}else{
	echo '<pre style="text-align: left; font-weight: bold; font-size: 10px; background-color: #ececec;">
	DEBUG:';
	foreach( $msgs as $msg ) {
		if( is_bool( $msg ) ) {
			$msg = 'BOOLEAN [' . ($msg ? 'T' : 'F' ) . ']';
		}else if( is_null( $msg ) ) {
			$msg = '__NULL__';
		}else if( is_string( $msg ) && !strlen( $msg ) ) {
			$msg = '__EMPTY_STRING__';
		}else if( is_array( $msg ) || is_object( $msg ) ) {
			$msg = print_r( $msg, true );
		}
		echo $msg . "\n\n";
	}
	echo '</pre>';
}
}
?>

Link to comment
Share on other sites

MORE PROGRESS!

 

Page load, empty dbase...

 

     

DEBUG:POST: 

Array
(
)


BOOLEAN [F]

 

After submitting three rows... (still nothing in the form after the submit, unfortunately)

 

     
DEBUG:POST: 

Array
(
    [player] => Array
        (
            [-1] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 9
                    [player_roster_lname] => 9
                    [player_roster_address] => 9
                    [player_roster_city] => 9
                    [player_roster_state] => 9
                    [player_roster_zip] => 9
                    [player_roster_phone] => 9
                    [player_roster_email] => 9
                    [player_roster_number] => 9
                    [player_roster_gradyear] => 9
                    [player_roster_height_feet] => 9
                    [player_roster_height_inches] => 9
                )

            [-2] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 7
                    [player_roster_lname] => 7
                    [player_roster_address] => 7
                    [player_roster_city] => 7
                    [player_roster_state] => 7
                    [player_roster_zip] => 7
                    [player_roster_phone] => 7
                    [player_roster_email] => 7
                    [player_roster_number] => 7
                    [player_roster_gradyear] => 7
                    [player_roster_height_feet] => 7
                    [player_roster_height_inches] => 7
                )

            [-3] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 5
                    [player_roster_lname] => 5
                    [player_roster_address] => 5
                    [player_roster_city] => 5
                    [player_roster_state] => 5
                    [player_roster_zip] => 5
                    [player_roster_phone] => 5
                    [player_roster_email] => 5
                    [player_roster_number] => 5
                    [player_roster_gradyear] => 5
                    [player_roster_height_feet] => 5
                    [player_roster_height_inches] => 5
                )

...etc...

            [-15] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

        )

    [editplayers] => Submit
)


Insert player

Insert player

Insert player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Array
(
    [player_id] => 1
    [customer_id] => 1112
    [roster_date] => 20090605200636
    [player_roster_fname] => 9
    [player_roster_lname] => 9
    [player_roster_address] => 9
    [player_roster_city] => 9
    [player_roster_state] => 9
    [player_roster_zip] => 9
    [player_roster_phone] => 9
    [player_roster_email] => 9
    [player_roster_number] => 9
    [player_roster_gradyear] => 9
    [player_roster_height_feet] => 9
    [player_roster_height_inches] => 9
)


Array
(
    [player_id] => 2
    [customer_id] => 1112
    [roster_date] => 20090605200636
    [player_roster_fname] => 7
    [player_roster_lname] => 7
    [player_roster_address] => 7
    [player_roster_city] => 7
    [player_roster_state] => 7
    [player_roster_zip] => 7
    [player_roster_phone] => 7
    [player_roster_email] => 7
    [player_roster_number] => 7
    [player_roster_gradyear] => 7
    [player_roster_height_feet] => 7
    [player_roster_height_inches] => 7
)


Array
(
    [player_id] => 3
    [customer_id] => 1112
    [roster_date] => 20090605200636
    [player_roster_fname] => 5
    [player_roster_lname] => 5
    [player_roster_address] => 5
    [player_roster_city] => 5
    [player_roster_state] => 5
    [player_roster_zip] => 5
    [player_roster_phone] => 5
    [player_roster_email] => 5
    [player_roster_number] => 5
    [player_roster_gradyear] => 5
    [player_roster_height_feet] => 5
    [player_roster_height_inches] => 5
)


BOOLEAN [F]

 

This time, after putting new data in the empty first row -- and only in the first row -- the debug post and the dbase confirm that the second and third rows are deleted, BUT the data for the first row is successfully replaced by the new data.

 

That, actually, is the way it should work.  If the rows had populated in the form, the effect of entering data only in the first row would cause rows 2 & 3 to delete in the dbase because the last input coming in from the form in rows 2 & 3 was empty fields.

 

So the thing we need is for the rows to load in the form.

 

Here is the debug output for the above scenario:

 

      DEBUG:POST: 

Array
(
    [player] => Array
        (
            [1] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => v
                    [player_roster_lname] => v
                    [player_roster_address] => v
                    [player_roster_city] => v
                    [player_roster_state] => v
                    [player_roster_zip] => v
                    [player_roster_phone] => v
                    [player_roster_email] => v
                    [player_roster_number] => v
                    [player_roster_gradyear] => v
                    [player_roster_height_feet] => v
                    [player_roster_height_inches] => v
                )

            [2] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [3] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-1] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-2] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-3] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

            [-4] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

...etc UP TO -12 BECAUSE WE HAVE 3 ROWS IN THE DBASE

            [-12] => Array
                (
                    [customer_id] => 1112
                    [player_roster_fname] => 
                    [player_roster_lname] => 
                    [player_roster_address] => 
                    [player_roster_city] => 
                    [player_roster_state] => 
                    [player_roster_zip] => 
                    [player_roster_phone] => 
                    [player_roster_email] => 
                    [player_roster_number] => 
                    [player_roster_gradyear] => 
                    [player_roster_height_feet] => 
                    [player_roster_height_inches] => 
                )

        )

    [editplayers] => Submit
)


Update player

Delete player

Delete player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Skipped player

Array
(
    [player_id] => 1
    [customer_id] => 1112
    [roster_date] => 20090605200935
    [player_roster_fname] => v
    [player_roster_lname] => v
    [player_roster_address] => v
    [player_roster_city] => v
    [player_roster_state] => v
    [player_roster_zip] => v
    [player_roster_phone] => v
    [player_roster_email] => v
    [player_roster_number] => v
    [player_roster_gradyear] => v
    [player_roster_height_feet] => v
    [player_roster_height_inches] => v
)


BOOLEAN [F]

 

Link to comment
Share on other sites

     echo tep_draw_input_field( "player[{$player['player_id']}][{$c}]", $row[$c], $extra ) ."</td>";

Should have been:

     echo tep_draw_input_field( "player[{$player['player_id']}][{$c}]", $player[$c], $extra ) ."</td>";

Notice the change from $row[$c] to $player[$c].  I'm used to calling my database rows $row in PHP, but you had named yours $player.  I was simply using the wrong variable.


<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
$navigation->set_snapshot();
tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php


mydbg( 'POST: ' ); mydbg( $_POST );//%%
if( !empty( $_POST ) ) {
/**
* If you print_r( $_POST ) you will find that you have an array named 'players'.
* Each index into this array will be POSITIVE and the players database ID if they already exist in the database
* The index will be NEGATIVE if the player is new and needs to be inserted.
*
* And then each player is an array where the associative names should match your column names,
* so that you can easily generate your insert / update statements based on what I
* showed you earlier.
*/
foreach( $_POST['player'] as $player_id => $player_info ) {
	// ADDED A CHECK FOR IF THE PLAYER IS EMPTY!
	$emptyplayer = $player_info; // Copy the array
	unset( $emptyplayer['customer_id'] ); // Remove customer_id from empty player
	$emptyplayer = trim( implode( " ", $emptyplayer ) );
	$emptyplayer = !strlen( $emptyplayer ); // true if empty, false otherwise
	foreach( $player_info as $k => $v ) {
		$player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
	}
	if( $player_id < 0 ) {
		if( $emptyplayer ) {
			mydbg( 'Skipped player' );//%%
			continue; // SKIP TO NEXT PLAYER
		}
		// OOPS!  SINCE 'player_id' IS AUTO-INCREMENTING, WE DO NOT HAVE TO INSERT IT.
		// THEREFORE THE FOLLOWING LINE IS COMMENTED OUT (i.e. you can remove it from your code).
		//$player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
		$stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
			. " ) values ( " . implode( ', ', $player_info ) . " )";
		mydbg( 'Insert player' );//%%
	}else if( !$emptyplayer ){
		// player_id GREATER THAN ZERO, SO PLAYER EXISTS IN DATABASE.  PLAYER IS NOT
		// EMPTY, SO WE UPDATE HIM!
		foreach( $player_info as $k => $v ) {
			// SLIGHTLY MORE READABLE
			$player_info[$k] = "`{$k}`={$v}";
		}
		$stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
			. "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		mydbg( 'Update player' );//%%
	}else{
		// player_id GREATER THAN ZERO SO HE EXISTS IN DATABASE.  PLAYER IS EMPTY
		// SO WE DELETE HIM!
		$stmt = "delete from `rosters` where `player_id`='" . mysql_real_escape_string( $player_id ) . "'";
		mydbg( 'Delete player' );//%%
	}
	$r = mysql_query( $stmt ); // check for errors and success
	if( !$r ) {
		mydbg( $stmt );//%%
		mydbg( mysql_error() );//%%
	}
}
}

echo "<form action=\"roster.php\" method=\"post\"><table>";

// The reason your players are not selected from the database and displayed in the form
// is because $sqlplayers is NULL; you are not running any query.
$sqlplayers = "select * from `rosters` where `customer_id`='" . mysql_real_escape_string( $customer_id ) . "'";
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="1"', 'height_inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
  $player = mysql_fetch_assoc( $result );
  mydbg( $player );//%%
}
   if( !$player ) {
  // We've run out of players, so create a blank one to insert
	$result = null; // stop trying to access result
  $player = $blankplayer;
	$player['player_id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   $firstcol = true;
   foreach( $cols as $c => $extra ) {
 $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
 echo "<td>";
 if( $firstcol === true ) {
	echo "<input type=\"hidden\" name=\"player[{$player['player_id']}][customer_id]\" value=\"{$customer_id}\" />";
 }
 echo tep_draw_input_field( "player[{$player['player_id']}][{$c}]", $player[$c], $extra ) ."</td>";
}
$player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<?php mydbg( null, false ); /*%%REMOVE ME dump debugging */?>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php
/**
* Simple debugging function.  If $add is true, it adds debugging message.  If $add is
* is false, it dumps debugging messages that were added.
*
* @param mixed $msg
* @param bool $add
*/
function mydbg( $msg, $add = true ) {
static $msgs = array();
if( $add === true ) {
	$msgs[] = $msg;
}else{
	echo '<pre style="text-align: left; font-weight: bold; font-size: 10px; background-color: #ececec;">
	DEBUG:';
	foreach( $msgs as $msg ) {
		if( is_bool( $msg ) ) {
			$msg = 'BOOLEAN [' . ($msg ? 'T' : 'F' ) . ']';
		}else if( is_null( $msg ) ) {
			$msg = '__NULL__';
		}else if( is_string( $msg ) && !strlen( $msg ) ) {
			$msg = '__EMPTY_STRING__';
		}else if( is_array( $msg ) || is_object( $msg ) ) {
			$msg = print_r( $msg, true );
		}
		echo $msg . "\n\n";
	}
	echo '</pre>';
}
}
?>

Link to comment
Share on other sites

WORKS!

 

I'm now going to add a separate but related form & sql for the table 'rosters_to_events.'  This table shows which events a coach has registered for, and therefore will allow me to get all the players for a coach for a particular event.. Hopefully I'll code this without screwing anything up!

 

Thanks again for all your help... it has been a pleasure!

 

And I will holla' at you if I get stuck!

Link to comment
Share on other sites

OK, I tried to get this added, but I need some help.

 

There is another table: rosters_to_events.  It hold rows that include customer_id (the coach's id), team_name, team_age, and the event_code(tournament) that the coach is submitting the roster for.

 

At the top of the player form you helped me with, I've added inputs for team_name, team_age, and the event_code.  customer_id is hidden field.

 

To get the rosters_to_event data into the form I did this:

 

$sqlgetevent = "SELECT customers_id, event_code, team_name, team_age FROM rosters_to_events WHERE customers_id = $customer_id";
$lastroster=mysql_query($sqlgetevent);
/*

if(mysql_query($sqlgetevent))>1 {
echo "too many results";
}
*/
$row = mysql_fetch_array($lastroster);
?>

<form action="roster.php" method="post"><table cellpadding="3"><tr bgcolor="#eeeeee"><td colspan="12"><table><tr><td>Team Name: <?php echo  tep_draw_input_field('team_name', $row['team_name'], 'size="10"'); ?></td><td>Age Level: <?php echo  tep_draw_input_field('team_age', $row['team_age'], 'size="10"'); ?></td><td>Event Code: <?php echo  tep_draw_input_field('event_code', $row['event_code'], 'size="10"'); ?></td></tr></table></td></tr>

 

(Right after this, the players field paint)

 

I manually put a row in the dbase, and the sql above puts it in the form correctly.  So far, so good.

 

Next, to insert/update data, I did this at the top of the page, right after checking to see if there are post variables...

 

if (!empty($lastroster)) {

$sqlinsevent="INSERT INTO rosters_to_events (customers_id, event_code, team_name, team_age) VALUES ('$customer_id', '$_POST[event_code]', '$_POST[team_name]', '$_POST[team_age]')";

mysql_query($sqlinsevent);

} else {

$sqlupdevent="UPDATE rosters_to_events SET event_code=$_POST[event_code], team_name=$_POST[$team_name], team_age=$_POST[$team_age] WHERE customers_id=$_POST[$customer_id]";

mysql_query($sqlupdevent);

}

 

When I make a change in the field values and hit submit, the page reloads with the revised value.  But when I reload the page, the old value is still there, and still unchanged in the dbase.

 

I think I may have an issue with single quotes and where they go in $_POST vars and arrays.  Seems that sometimes the whole thing is in single quotes '$_POST[foo]' , sometimes  the single quotes are in the string $_POST['foo'].

 

But there may be another thing I have overlooked as well.

 

What am I doing wrong?

Link to comment
Share on other sites

Here is the full code...

 

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
   $navigation->set_snapshot();
   tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php


mydbg( 'POST: ' ); mydbg( $_POST );//%%
if( !empty( $_POST ) ) {

if (!empty($lastroster)) {

$sqlinsevent="INSERT INTO rosters_to_events (customers_id, event_code, team_name, team_age) VALUES ('$customer_id', '$_POST[event_code]', '$_POST[team_name]', '$_POST[team_age]')";

mysql_query($sqlinsevent);

} else {
echo "update";

$sqlupdevent="UPDATE rosters_to_events SET event_code=$_POST[event_code], team_name=$_POST[$team_name], team_age=$_POST[$team_age] WHERE customers_id=$_POST[$customer_id]";

mysql_query($sqlupdevent);

}

/* 

if(!mysql_query($sqlinsevent)) {
die('InsEventError: ' . mysql_error());
} 

*/


   /**
   * If you print_r( $_POST ) you will find that you have an array named 'players'.
   * Each index into this array will be POSITIVE and the players database ID if they already exist in the database
   * The index will be NEGATIVE if the player is new and needs to be inserted.
   *
   * And then each player is an array where the associative names should match your column names,
   * so that you can easily generate your insert / update statements based on what I
   * showed you earlier.
   */
   foreach( $_POST['player'] as $player_id => $player_info ) {
      // ADDED A CHECK FOR IF THE PLAYER IS EMPTY!
      $emptyplayer = $player_info; // Copy the array
      unset( $emptyplayer['customer_id'] ); // Remove customer_id from empty player
      $emptyplayer = trim( implode( " ", $emptyplayer ) );
      $emptyplayer = !strlen( $emptyplayer ); // true if empty, false otherwise
      foreach( $player_info as $k => $v ) {
         $player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
      }
      if( $player_id < 0 ) {
         if( $emptyplayer ) {
            mydbg( 'Skipped player' );//%%
            continue; // SKIP TO NEXT PLAYER
         }
         // OOPS!  SINCE 'player_id' IS AUTO-INCREMENTING, WE DO NOT HAVE TO INSERT IT.
         // THEREFORE THE FOLLOWING LINE IS COMMENTED OUT (i.e. you can remove it from your code).
         //$player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
         $stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
            . " ) values ( " . implode( ', ', $player_info ) . " )";
         mydbg( 'Insert player' );//%%
      }else if( !$emptyplayer ){
         // player_id GREATER THAN ZERO, SO PLAYER EXISTS IN DATABASE.  PLAYER IS NOT
         // EMPTY, SO WE UPDATE HIM!
         foreach( $player_info as $k => $v ) {
            // SLIGHTLY MORE READABLE
            $player_info[$k] = "`{$k}`={$v}";
         }
         $stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
            . "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
         mydbg( 'Update player' );//%%
      }else{
         // player_id GREATER THAN ZERO SO HE EXISTS IN DATABASE.  PLAYER IS EMPTY
         // SO WE DELETE HIM!
         $stmt = "delete from `rosters` where `player_id`='" . mysql_real_escape_string( $player_id ) . "'";
         mydbg( 'Delete player' );//%%
      }
      $r = mysql_query( $stmt ); // check for errors and success
      if( !$r ) {
         mydbg( $stmt );//%%
         mydbg( mysql_error() );//%%
      }
   }
}

$sqlgetevent = "SELECT customers_id, event_code, team_name, team_age FROM rosters_to_events WHERE customers_id = $customer_id";
$lastroster=mysql_query($sqlgetevent);
/*

if(mysql_query($sqlgetevent))>1 {
echo "too many results";
}
*/
$row = mysql_fetch_array($lastroster);
?>

<form action="roster.php" method="post"><table cellpadding="3"><tr bgcolor="#eeeeee"><td colspan="12"><table><tr><td>Team Name: <?php echo  tep_draw_input_field('team_name', $row['team_name'], 'size="10"'); ?></td><td>Age Level: <?php echo  tep_draw_input_field('team_age', $row['team_age'], 'size="10"'); ?></td><td>Event Code: <?php echo  tep_draw_input_field('event_code', $row['event_code'], 'size="10"'); ?></td></tr></table></td></tr><tr><td class="headerNavigation">Player First Name</td><td class="headerNavigation">Player Last Name</td><td class="headerNavigation">Player Address</td><td class="headerNavigation">City</td><td class="headerNavigation">State</td><td class="headerNavigation">Zip</td><td class="headerNavigation">Player Phone</td><td class="headerNavigation">Player Email</td><td class="headerNavigation">Jersey #</td><td class="headerNavigation">Grad Yr</td><td class="headerNavigation">Ht. Ft.</td><td class="headerNavigation">Ht. In.</td></tr>

<?php

// The reason your players are not selected from the database and displayed in the form
// is because $sqlplayers is NULL; you are not running any query.
$sqlplayers = "select * from `rosters` where `customer_id`='" . mysql_real_escape_string( $customer_id ) . "'";
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="15"', 'lname' => 'size="15"', 'address' => 'size="20"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="2"', 'height_inches' => 'size="2"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
     $player = mysql_fetch_assoc( $result );
     mydbg( $player );//%%
   }
   if( !$player ) {
     // We've run out of players, so create a blank one to insert
      $result = null; // stop trying to access result
     $player = $blankplayer;
      $player['player_id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   $firstcol = true;
   foreach( $cols as $c => $extra ) {
    $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
    echo "<td>";
    if( $firstcol === true ) {
      echo "<input type=\"hidden\" name=\"player[{$player['player_id']}][customer_id]\" value=\"{$customer_id}\" />";
    }
    echo tep_draw_input_field( "player[{$player['player_id']}][{$c}]", $player[$c], $extra ) ."</td>";
   }
   $player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<?php mydbg( null, false ); /*%%REMOVE ME dump debugging */?>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php
/**
* Simple debugging function.  If $add is true, it adds debugging message.  If $add is
* is false, it dumps debugging messages that were added.
*
* @param mixed $msg
* @param bool $add
*/
function mydbg( $msg, $add = true ) {
   static $msgs = array();
   if( $add === true ) {
      $msgs[] = $msg;
   }else{
      echo '<pre style="text-align: left; font-weight: bold; font-size: 10px; background-color: #ececec;">
      DEBUG:';
      foreach( $msgs as $msg ) {
         if( is_bool( $msg ) ) {
            $msg = 'BOOLEAN [' . ($msg ? 'T' : 'F' ) . ']';
         }else if( is_null( $msg ) ) {
            $msg = '__NULL__';
         }else if( is_string( $msg ) && !strlen( $msg ) ) {
            $msg = '__EMPTY_STRING__';
         }else if( is_array( $msg ) || is_object( $msg ) ) {
            $msg = print_r( $msg, true );
         }
         echo $msg . "\n\n";
      }
      echo '</pre>';
   }
}
?>

Link to comment
Share on other sites

Did some troubleshooting and fixed a few things using error checking.

 

Right now the insert statement for the rosters_to_events table is working, but when I modify the fields, it is not updating, just inserting a new row.

 

Laqtest code...

 

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
   $navigation->set_snapshot();
   tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php

// echo $customer_id;
echo $row['customers_id'];

mydbg( 'POST: ' ); mydbg( $_POST );//%%
if( !empty( $_POST ) ) {

if (!$lastroster) {

echo "insert";

$sqlinsevent="INSERT INTO rosters_to_events (customers_id, event_code, team_name, team_age) VALUES ('$customer_id', '$_POST[event_code]', '$_POST[team_name]', '$_POST[team_age]')";

if(!mysql_query($sqlinsevent)) {
die('InsertEventError: ' . mysql_error());
} 

} else {
echo "update";

$sqlupdevent="UPDATE rosters_to_events SET event_code=$_POST[event_code], team_name=$_POST[$team_name], team_age=$_POST[$team_age] WHERE customers_id=$_POST[customer_id]";

if(!mysql_query($sqlupdevent)) {
die('UpdateEventError: ' . mysql_error());
} 

}

/* 

if(!mysql_query($sqlinsevent)) {
die('InsEventError: ' . mysql_error());
} 

*/


   /**
   * If you print_r( $_POST ) you will find that you have an array named 'players'.
   * Each index into this array will be POSITIVE and the players database ID if they already exist in the database
   * The index will be NEGATIVE if the player is new and needs to be inserted.
   *
   * And then each player is an array where the associative names should match your column names,
   * so that you can easily generate your insert / update statements based on what I
   * showed you earlier.
   */
   foreach( $_POST['player'] as $player_id => $player_info ) {
      // ADDED A CHECK FOR IF THE PLAYER IS EMPTY!
      $emptyplayer = $player_info; // Copy the array
      unset( $emptyplayer['customer_id'] ); // Remove customer_id from empty player
      $emptyplayer = trim( implode( " ", $emptyplayer ) );
      $emptyplayer = !strlen( $emptyplayer ); // true if empty, false otherwise
      foreach( $player_info as $k => $v ) {
         $player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
      }
      if( $player_id < 0 ) {
         if( $emptyplayer ) {
            mydbg( 'Skipped player' );//%%
            continue; // SKIP TO NEXT PLAYER
         }
         // OOPS!  SINCE 'player_id' IS AUTO-INCREMENTING, WE DO NOT HAVE TO INSERT IT.
         // THEREFORE THE FOLLOWING LINE IS COMMENTED OUT (i.e. you can remove it from your code).
         //$player_info['player_id'] = "'" . mysql_real_escape_string( $player_id ) . "'";
         $stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
            . " ) values ( " . implode( ', ', $player_info ) . " )";
         mydbg( 'Insert player' );//%%
      }else if( !$emptyplayer ){
         // player_id GREATER THAN ZERO, SO PLAYER EXISTS IN DATABASE.  PLAYER IS NOT
         // EMPTY, SO WE UPDATE HIM!
         foreach( $player_info as $k => $v ) {
            // SLIGHTLY MORE READABLE
            $player_info[$k] = "`{$k}`={$v}";
         }
         $stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
            . "`player_id`='" . mysql_real_escape_string( $player_id ) . "'";
         mydbg( 'Update player' );//%%
      }else{
         // player_id GREATER THAN ZERO SO HE EXISTS IN DATABASE.  PLAYER IS EMPTY
         // SO WE DELETE HIM!
         $stmt = "delete from `rosters` where `player_id`='" . mysql_real_escape_string( $player_id ) . "'";
         mydbg( 'Delete player' );//%%
      }
      $r = mysql_query( $stmt ); // check for errors and success
      if( !$r ) {
         mydbg( $stmt );//%%
         mydbg( mysql_error() );//%%
      }
   }
}

$sqlgetevent = "SELECT customers_id, event_code, team_name, team_age FROM rosters_to_events WHERE customers_id = $customer_id";
$lastroster=mysql_query($sqlgetevent);
/*

if(mysql_query($sqlgetevent))>1 {
echo "too many results";
}
*/
$row = mysql_fetch_array($lastroster);
// echo $row['customers_id'];
?>

<form action="roster.php" method="post"><table cellpadding="3"><tr bgcolor="#eeeeee"><td colspan="12"><table><tr><td>Team Name: <?php echo  tep_draw_input_field('team_name', $row['team_name'], 'size="10"'); ?></td><td>Age Level: <?php echo  tep_draw_input_field('team_age', $row['team_age'], 'size="10"'); ?></td><td>Event Code: <?php echo  tep_draw_input_field('event_code', $row['event_code'], 'size="10"'); ?></td></tr></table></td></tr><tr><td class="headerNavigation">Player First Name</td><td class="headerNavigation">Player Last Name</td><td class="headerNavigation">Player Address</td><td class="headerNavigation">City</td><td class="headerNavigation">State</td><td class="headerNavigation">Zip</td><td class="headerNavigation">Player Phone</td><td class="headerNavigation">Player Email</td><td class="headerNavigation">Jersey #</td><td class="headerNavigation">Grad Yr</td><td class="headerNavigation">Ht. Ft.</td><td class="headerNavigation">Ht. In.</td></tr>

<?php

// The reason your players are not selected from the database and displayed in the form
// is because $sqlplayers is NULL; you are not running any query.
$sqlplayers = "select * from `rosters` where `customer_id`='" . mysql_real_escape_string( $customer_id ) . "'";
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="15"', 'lname' => 'size="15"', 'address' => 'size="20"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'height_feet' => 'size="2"', 'height_inches' => 'size="2"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
     $player = mysql_fetch_assoc( $result );
     mydbg( $player );//%%
   }
   if( !$player ) {
     // We've run out of players, so create a blank one to insert
      $result = null; // stop trying to access result
     $player = $blankplayer;
      $player['player_id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   $firstcol = true;
   foreach( $cols as $c => $extra ) {
    $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
    echo "<td>";
    if( $firstcol === true ) {
      echo "<input type=\"hidden\" name=\"player[{$player['player_id']}][customer_id]\" value=\"{$customer_id}\" />";
    }
    echo tep_draw_input_field( "player[{$player['player_id']}][{$c}]", $player[$c], $extra ) ."</td>";
   }
   $player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<?php mydbg( null, false ); /*%%REMOVE ME dump debugging */?>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php
/**
* Simple debugging function.  If $add is true, it adds debugging message.  If $add is
* is false, it dumps debugging messages that were added.
*
* @param mixed $msg
* @param bool $add
*/
function mydbg( $msg, $add = true ) {
   static $msgs = array();
   if( $add === true ) {
      $msgs[] = $msg;
   }else{
      echo '<pre style="text-align: left; font-weight: bold; font-size: 10px; background-color: #ececec;">
      DEBUG:';
      foreach( $msgs as $msg ) {
         if( is_bool( $msg ) ) {
            $msg = 'BOOLEAN [' . ($msg ? 'T' : 'F' ) . ']';
         }else if( is_null( $msg ) ) {
            $msg = '__NULL__';
         }else if( is_string( $msg ) && !strlen( $msg ) ) {
            $msg = '__EMPTY_STRING__';
         }else if( is_array( $msg ) || is_object( $msg ) ) {
            $msg = print_r( $msg, true );
         }
         echo $msg . "\n\n";
      }
      echo '</pre>';
   }
}
?>

Link to comment
Share on other sites

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.