3

我是 PHP 新手,整天都在努力解决这个问题。我有一个包含玩家详细信息的 MySQL 数据库。我想从数据库中选择它们并以表格的形式显示在表格中,这适用于下面的代码。

当我想更新行时,问题就出现了,可能是使用选项字段将播放器状态从活动状态变为非活动状态。当我按下提交时,我可以获得每个 pdbactive 字段的值 OK,但我无法获得 PID,这是对数据库进行更新的关键。

<form name="selected_players" action="" method="POST">
            <table border="1">
            <th>Player ID</th>
            <th>Player Name</th>
            <th>Player Surname</th>
            <th>Status</th>
            <th>Primary Team*</th>
            <?php
            // Then get the players from the players table who have got same team_name but no team_id yet....which would have be set if they were active
            $query = "select * from `player2012` where teamid ='$tid' and aru='A';";     
            $result = mysql_query($query) or die ("Error in query: $query " . mysql_error()); 
            $count = mysql_num_rows($result);  
            echo $count;
            //echo ("<br />");

            while ($row = mysql_fetch_assoc($result)) {

                    $player_id = $row["player_id"];     
                    $player_fname = $row["player_fname"];
                    $player_sname = $row["player_sname"];
                    $pactive = $row["active"];  
                    //echo ($pactive);
                    //echo ("<br />");  
                    ?>
                     <tr>
                        <td><input name"pid[]" id="pid[]" type="text" value="<?php echo $player_id ?>" /><?php echo $player_id ?></td>
                        <td><input name"player_fn[]" type="hidden" value="<?php echo $player_fname ?>" /><?php echo $player_fname ?></td>
                        <td><input name"player_sn[]" type="hidden" value="<?php echo $player_sname ?>" /><?php echo $player_sname ?></td>

                        <?php if($pactive=="1"){
                            ?>
                                <td><select name="pdbactive[]" id="pdbactive[]">
                                <option value="1" selected="selected">Active</option>
                                <option value="0">Not Active</option>
                                </td>
                        <?php  }
                        else
                        {
                            ?>
                                <td><select name="pdbactive[]" id="pdbactive[]">
                                <option value="1">Active</option>
                                <option value="0" selected="selected">Not Active</option>
                        </td>
                        <?php
                        }
                        ?>

                        <td>&nbsp;
                        </td>
                    </tr>
                 <?php
                     }
                  ?>
                </table>
            <p>&nbsp;</p>
            <p> When you have updated "status" for each of these players, please press submit button 
                <input name="Submit" type="submit" id="Sumbit" value="Submit" /> </p>
            </form>

            <?php

            mysql_free_result($result); 

            //if form has been pressed proccess it
            if($_POST["Submit"])
            {
                    //get data from form
                    $ractive = $_POST['pdbactive'];
                    $rid= $_POST['pid'];

                    for($i=0;$i<$count;$i++){
                            echo ($i);
                            echo ("<br />");
                            $stat=($ractive[$i]);
                            $idd=($rid[$i]);

                            $sql_insert="UPDATE 'player2012' SET active='$stat' where id='$idd'";
                            print($sql_insert);
                            echo ("<br />");
                            }
            }

            ?>

目前我只想创建 SQL 语句,以便在开始更新数据库之前知道它可以工作。所以所有的回声和打印只是我试图找出答案

帮助!!!!!!!!!!!!!!

4

3 回答 3

2

What's the exact name of the table you're trying to insert into ? (case-sensitive wise). If it's lowercase like in:
UPDATE 'player2012' SET active='$stat' where id='$idd'

- it should be fine, but it it's uppercase - you should remove the quotes:

UPDATE player2012 SET active='$stat' where id='$idd'.

Also, I would add: echo $rid; and inside the for-loop: echo $idd; to see what's the parameters I'm getting.

于 2012-07-19T21:41:07.397 回答
0

解决了。

问题是我在表单输入名称上缺少一个“=”

<td><input name"pid[]" id="pid[]" type="text".....

应该读

<td><input name="pid[]" id="pid[]" type="text"....

微妙但现在可以享受!

于 2012-07-20T09:56:36.930 回答
0

我不是 100% 确定你在寻找什么,但我认为你想要一个选项,比如每个播放器旁边的复选框,如果选中了那个框并且你点击提交,它会删除那个播放器?

于 2012-07-19T21:38:17.193 回答