如何使用php从多行表单更新mysql数据库
我是PHP新手,整天都在努力解决这个问题。我有一个包含玩家详细信息的MySQL数据库。我想从数据库中选择它们,并以表单的形式显示在表中,这与下面的代码配合得很好 当我想更新行时,问题来了,可能是使用选项字段将玩家状态从活动变为非活动。 当我按下submit键时,我可以得到每个pdbactive字段的值,但是我不能得到PID,PID是在数据库中进行升级的关键如何使用php从多行表单更新mysql数据库,php,mysql,html,forms,Php,Mysql,Html,Forms,我是PHP新手,整天都在努力解决这个问题。我有一个包含玩家详细信息的MySQL数据库。我想从数据库中选择它们,并以表单的形式显示在表中,这与下面的代码配合得很好 当我想更新行时,问题来了,可能是使用选项字段将玩家状态从活动变为非活动。 当我按下submit键时,我可以得到每个pdbactive字段的值,但是我不能得到PID,PID是在数据库中进行升级的关键 <form name="selected_players" action="" method="POST">
<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>
</td>
</tr>
<?php
}
?>
</table>
<p> </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 />");
}
}
?>
玩家ID
球员姓名
玩家姓氏
地位
初级队*
我不是100%确定你在寻找什么,但我认为你希望在每个玩家旁边都有一个复选框这样的选项,如果选中该复选框并单击“提交”,它将删除该玩家?你试图插入的表的确切名称是什么?(区分大小写)。
如果它是小写的,如:
UPDATE'player2012'SET active='$stat'其中id='$idd'
-应该可以,但它是大写的-您应该删除引号:
UPDATE player2012 SET active='$stat'其中id='$idd'
此外,我还要添加:echo$rid代码>和for循环内部:echo$idd
查看我得到的参数是什么。解决了它
问题是我在表单输入名称的
<td><input name"pid[]" id="pid[]" type="text".....
Hi David,是的,你是对的,我遇到的问题是当我按submit更新数据库时从表单中获取信息。我可以得到pdbactive OK,但是我没有得到pid?
<td><input name="pid[]" id="pid[]" type="text"....