每个用户

时间:2015-08-31 15:59:42

标签: php mysql forms

我试图设计一个页面,我可以手动输入每个用户的播放器名称并将其发送到我的数据库表中。

每个用户有13个不同的玩家,他们是我上传的图像上的蓝色污迹。玩家将被输入我在他们之下的输入中。

enter image description here

我很难弄清楚每个用户如何INSERT玩家进入我的数据库。因此,如果我在第4块/输入中的第一个用户下,并点击提交,我希望用第4个选择分配给该用户。

这是我的数据库结构......

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

这是我想要做的代码。

        <form action="" method="POST"> 
<?php 
$con = mysqli_connect("localhost", "", "", ""); 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", 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>

任何人都可以提供一些有关我做错了什么或如何做到这一点的见解吗?

0 个答案:

没有答案
相关问题