2012-10-13 74 views
3

我正在運行while循環並從數據庫中獲取3條記錄。然後在同一頁面上更新它。每個記錄都有提交按鈕。但編輯完成後,當我提交表單時,它只捕獲最後一條記錄的值,並用最後一條記錄值更新其他行。如果有人幫助我,我會非常感激。記住它捕獲確切的(id),但其他參數只是最後一行。PHP表格更新更新形式

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


    } 

回答

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 
    } ?> 
+0

感謝。 –

+0

@ZainAbid歡迎:) –

0

你需要做的,而輸入數組,因爲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']?>?>')"/> 

現在在PHP中你會得到如下

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

// same way you can do your insert or update. 
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"); 


    }