更新表单中的 Php 表单更新

标签 php html forms phpmyadmin

我正在运行 while 循环并从数据库中获取 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 行都有相同的名称。

解决方案:-

在 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 
    } ?>

关于更新表单中的 Php 表单更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872134/

相关文章:

javascript - 在 $(document).ready() 中定义的调用函数

html - 如何使用此模板搜索按钮代替表单中的提交按钮?

php - 选择框中的选项没有响应

javascript - Javascript 中表格上的“保存”按钮不起作用

php - 用于查找 sibling 的简单 dom 解析器

html - 谷歌浏览器在刷新时调整字体大小

php - 如何在不重定向到该表单操作页面的情况下处理表单操作的结果?

php - 防止 PHP 注册表单中出现重复的电子邮件地址

php - 使用 php 进行数据库备份的 mysqldump 不起作用

php/Mysql最好的树结构