I'm attempting to design a page that I can manually enter player names per user and send that into my database table.
Each user has 13 different players and they are the blue smudges on the image I uploaded. There player's will be entered in the inputs I have under neath of them.
I am having difficulty figuring out how I can INSERT
a player into my database per user. So if I was under the first user in the 4th block/input, and clicked submit, I would want that to be assigned to that user with the 4th pick.
Here is my database structure...
playersByUser
CREATE TABLE `playersByUser` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) DEFAULT NULL,
`playername` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`ordering` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `userid` (`userid`),
CONSTRAINT `playersByUser_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Here is the code for what I am trying to do.
<form action="" method="POST">
<?php
$con = mysqli_connect("localhost", "", "", "");
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$stmt = mysqli_query($con,"SELECT up.ordering, u.username, up.playername
FROM users AS u
INNER JOIN playersByUser AS up ON u.id = up.userid
WHERE u.id = $userid
ORDER BY up.ordering");
if(isset($_POST['Add Player'])){
//Variables to post in the bind_param
$insert_user_id = $_POST['user_id'];
$insert_player = $_POST['playername'];
$stmt2 = $con->prepare("INSERT INTO playersByUser (user_id, playername) VALUES (?, ?)");
if ( false===$stmt2 ) {
die('Player Insert prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt2->bind_param('is', $insert_user_id, $insert_player)) {
// Check errors for binding parameters
die('Player Insert bind_param() failed: ' . htmlspecialchars($stmt2->error));
}
if(!$stmt2->execute()) {
die('Player Insert execute() failed: ' . htmlspecialchars($stmt2->error));
}
}
$playerArray = array();
//$userid => $insert_player
//);
while($row = mysqli_fetch_array($stmt)) {
$playersArray[$row['userid']=$row['playername'];
?>
<div class="draftResultsWrap">
<div class="inline">
<?php echo "<div>" . $row['firstname'] . " " . $row['lastname'] . "</div>"; ?>
</div>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='1'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='2'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='3'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='4'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='5'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='6'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='7'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='8'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='9'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='10'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='11'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='12'/>
<input type="text" name="<?php echo playerArray[] ?>" class="draftBorder" value='13'/>
</div>
<?php
}
?>
<button type="submit" method="POST" name="Add Player">Submit Changes</button>
</form>
Could anyone provide some insight on what I am doing wrong or how to do this?