Php表单更新更新表单

时间:2012-10-13 10:15:06

标签: php html forms phpmyadmin

我正在循环运行并从数据库中获取3条记录。然后在同一页面上更新它。每条记录都有提交按钮。但是在我提交表单后进行编辑时,它仅捕获最后一条记录的值,并使用最后一条记录值更新其他行。如果有人帮助我,我会非常感激。记住它捕获精确(id)但其他参数只是最后一行。

<form method="post" action="">
    <table width="700" border="1">
       <tr><th><?php echo $_SESSION['teamtwo']; ?></th></tr>
       <tr>
         <th>Player Name</th>
         <th>Runs</th>
         <th>Edit</th>
         <th>Save</th>
       </tr>
       <?php
        $team = new DBConnection();
        $condition = "WHERE teamname = '".$_SESSION['teamtwo']."' and datecreated = CURDATE()";
        $sel_player = $team->SelectRecord(array("*"),"`match`","$condition");
        //$sel_player = mysql_query("SELECT * FROM `match` WHERE teamname = '$team1' and datecreated = CURDATE()") or die(mysql_error());
        while($get_player = mysql_fetch_array($sel_player))
        {
        $totalruns = $get_player['runs_bat'];
        $totalballs = $get_player['ball_bat'];
        @$strike = $totalruns / $totalballs * 100; 
        ?>
        <tr>
          <td><input type="text" name="player_name" value="<?php echo $get_player['player_name']; ?>" disabled="disabled" /></td>
           <td><input type="text" name="runs" value="<?php echo $get_player['runs_bat']; ?>" size="1" /></td>

           <td><button><a href="?player=<?php echo $get_player['id']; ?>">Edit</a></button></td>
           <td><input type="submit" value="Save" name="team" /></td>
             </tr>
         <?php 
        } ?>
     </table>
   </form>
<?php } ?>
 </div>
 </div>
</body>
</html>
<?php

    if(isset($_POST['team'])){
        $runs = $_POST['runs'];
        $balls = $_POST['ball'];




        $object = new DBConnection();
        $arr_Field=array("runs_bat","ball_bat","player_status","how_out","opposite_bowl","opposite_player","sr","overs","bowl_ball","runs_ball","extra","madien");
        $arr_Values=array("$runs","$balls","$status","$how_out","$opposite_bowler","$opposite_player","$sr","$over","$bowls","$score","$extra","$madien");
        $condition = "WHERE id = '".$_REQUEST['player']."'";
        //echo $_REQUEST['player'];

        //echo $runs.$balls;

        $object->UpdateRecord("`match`",$arr_Field,$arr_Values,"$condition") or die(mysql_error());
        //header("Location:extra.php?update");


    }

3 个答案:

答案 0 :(得分:2)

问题是你有一个表单,当你提交表单时,它会提交最后一行的值,因为你对1个表单中的所有3行都有相同的名称。

<强>解决方案: -

在while循环中创建表单元素,并在while循环内部关闭它。像这样,你将有3个表格,每行3行。

代码示例: -

    while($get_player = mysql_fetch_array($sel_player))
    {
    $totalruns = $get_player['runs_bat'];
    $totalballs = $get_player['ball_bat'];
    @$strike = $totalruns / $totalballs * 100; 
    ?>
    <form>
    <tr>
      <td><input type="text" name="player_name" value="<?php echo $get_player['player_name']; ?>" disabled="disabled" /></td>
       <td><input type="text" name="runs" value="<?php echo $get_player['runs_bat']; ?>" size="1" /></td>

       <td><button><a href="?player=<?php echo $get_player['id']; ?>">Edit</a></button></td>
       <td><input type="submit" value="Save" name="team" /></td>
    </tr>
    </form>
     <?php 
    } ?>

答案 1 :(得分:0)

<强> 1 你需要输入数组,因为name属性在循环中被覆盖

<td><input type="text" name="player_name[<?php echo $get_player['id']?>]" value="<?php echo $get_player['player_name']; ?>" disabled="disabled" /></td>
<td><input type="text" name="runs[<?php echo $get_player['id']?>]" value="<?php echo $get_player['runs_bat']; ?>" size="1" /></td>

<强> 2 你有所有文本框意味着如果按下一行的提交按钮,那么你也会得到所有文本框作为php方面,所以在表单中创建隐藏变量,以获得点击的按钮

//write javascript in your page
<script>
function setPlayerId(id) {
  document.getElementById('playerid').value=id;
}
</script>

//take hidden field into form
<input type='hidden' name='playerid' value='0'>

//write down onlick button event

<input type="submit" value="Save" name="team" onClick="setPlayerId('<?php <?php echo $get_player['id']?>?>')"/>

第3  现在在php中你将得到如下

echo $_POST['player_name'][$_POST['playerid']];

// same way you can do your insert or update.

答案 2 :(得分:0)

此代码必须正常工作

<form method="post" action="">
    <table width="700" border="1">
       <tr><th><?php echo $_SESSION['teamtwo']; ?></th></tr>
       <tr>
         <th>Player Name</th>
         <th>Runs</th>
         <th>Edit</th>
         <th>Save</th>
       </tr>
       <?php
        $team = new DBConnection();
        $condition = "WHERE teamname = '".$_SESSION['teamtwo']."' and datecreated = CURDATE()";
        $sel_player = $team->SelectRecord(array("*"),"`match`","$condition");
        //$sel_player = mysql_query("SELECT * FROM `match` WHERE teamname = '$team1' and datecreated = CURDATE()") or die(mysql_error());
        while($get_player = mysql_fetch_array($sel_player))
        {
        $totalruns = $get_player['runs_bat'];
        $totalballs = $get_player['ball_bat'];
        @$strike = $totalruns / $totalballs * 100; 
        ?>
        <tr>
          <td><input type="text" name="player_name" value="<?php echo $get_player['player_name']; ?>" disabled="disabled" /></td>
           <td><input type="text" name="runs<?=$get_player['id']?>" value="<?php echo $get_player['runs_bat']; ?>" size="1" /></td>
// you didnt write this i added
<input type="text" name="ball<?=$get_player['id']?>" value="<?php echo $get_player['ball_bat']; ?>" size="1" />

           <td><button><a href="?player=<?php echo $get_player['id']; ?>">Edit</a></button></td>
           <td><input type="submit" value="Save" name="team" /></td>
             </tr>
         <?php 
        } ?>
     </table>
   </form>
<?php } ?>
 </div>
 </div>
</body>
</html>
<?php

    if(isset($_POST['team'])){
    $runsname = 'runs'.$_GET['player'];
    $ballsname = 'ball'.$_GET['player'];
        $runs = $_POST[$runsname];
        $balls = $_POST[$ballsname];




        $object = new DBConnection();
        $arr_Field=array("runs_bat","ball_bat","player_status","how_out","opposite_bowl","opposite_player","sr","overs","bowl_ball","runs_ball","extra","madien");
        $arr_Values=array("$runs","$balls","$status","$how_out","$opposite_bowler","$opposite_player","$sr","$over","$bowls","$score","$extra","$madien");
        $condition = "WHERE id = '".$_REQUEST['player']."'";
        //echo $_REQUEST['player'];

        //echo $runs.$balls;

        $object->UpdateRecord("`match`",$arr_Field,$arr_Values,"$condition") or die(mysql_error());
        //header("Location:extra.php?update");


    }