User enters player information into form (1-12 players at a time)
$player query: checks for duplicate player and either enters a new player or updates a player already in the players table.
$pid query: gets the matching player's ID ($playerID)
$roster query: inserts roster information to the rosters table - schoolID, playerID, uniform, and if he's on the varsity level or not
The $player query works great!
The $pid query...does a good job getting the $playerID (I echo it out)
Where my problem comes is I can't seem to get the $roster query to recognize $playerID.
$schoolID = $_POST['school'];
$varsity = $_POST['varsity'];
$season = $_POST['season'];
$player = $con->prepare("INSERT INTO a_players(schoolID,nameFirst,nameLast,feet,inches,grade,position,varsity)
VALUES (?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE
feet = ?,
inches = ?,
varsity = ?
");
$player->bind_param('sssssssssss',$schoolID,$fname,$lname,$feet,$inches,$grade,$position,$varsity,$feet,$inches,$varsity);
$pid = $con->prepare("SELECT id FROM a_players WHERE nameFirst = ? AND nameLast = ? AND schoolID = ?");
$pid->bind_param('sss',$fname,$lname,$schoolID);
$roster = $con->prepare("INSERT INTO a_rosters(schoolID, playerID, uniform, varsity, season) VALUES (?,?,?,?,?)");
$roster->bind_param('sssss', $schoolID, $playerID, $uniform, $varsity, $season);
foreach ($_POST['uniform'] as $k => $uniform) {
$fname = $_POST['nameFirst'][$k];
$lname = $_POST['nameLast'][$k];
$feet = $_POST['feet'][$k];
$inches = $_POST['inches'][$k];
$grade = $_POST['grade'][$k];
$position = $_POST['position'][$k];
$name = "$fname $lname";
$slug = strtolower("$fname-$lname");
$taxonomy = "post_tag";
$player->execute();
echo '<div>' .$fname,$lname,$schoolID .'</div>';
$pid->execute();
$pid->bind_result($playerID);
$pid->fetch();
echo '<div>playerID: ' . $playerID .' value of row: '. $row .'</div>';
$roster->execute();
The echos around $pid->execute are just there to see what's being passed. Once it's working, those get removed.
My Answer:
It looks like the issue is with the $playerID variable not being set correctly before executing the $roster query.
You need to fetch the result of the $pid query before using the $playerID variable in the $roster query.
Here's how you can modify your code:
php
$player->execute();
$pid->execute();
$pid->bind_result($playerID);
$pid->fetch();
echo 'playerID: ' . $playerID . ' value of row: ' . $row . '';
$roster->bind_param('sssss', $schoolID, $playerID, $uniform, $varsity, $season);
$roster->execute();
By fetching the result of the $pid query before using the $playerID variable in the $roster query, you should be able to insert the roster information correctly into the rosters table.
Rate this post
4 of 5 based on 8012 votesComments