3

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");


    }
4

3 に答える 3

2

問題は、1 つのフォームがあり、フォームを送信すると、1 つのフォーム内の 3 つの行すべてに同じ名前が付けられているため、最後の行の値が送信されることです。

解決:-

while ループ内で form 要素を作成し、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 
    } ?>
于 2012-10-13T10:36:53.393 に答える
0

1. name 属性がループで上書きされるため、while で入力配列を作成する必要があります。

<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. すべてのテキストボックスは、1行の送信ボタンを押すと意味があり、すべてのテキストボックスを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.
于 2012-10-13T10:36:06.327 に答える
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");


    }
于 2012-10-13T10:38:24.413 に答える