2011-12-23 173 views
1

我想使用复选框更改sql表中的数据。我已经成功地回显了我的表格的所有数据。但是,我无法删除。我输入新数据后点击复选框并提交。数据返回到有价值的数据。任何人都可以帮助我吗?非常感谢。使用复选框更改数据

<?php 


include_once("mesparametres.inc.php"); 
$sql="SELECT * FROM poisson"; 
$result=mysql_query($sql); 

// Count table rows 
$count=mysql_num_rows($result); 
?> 
<table width="500" border="0" cellspacing="1" cellpadding="0"> 
<form name="form1" method="post" action="modifier.php"> 
<tr> 
<td> 
<table width="500" border="0" cellspacing="1" cellpadding="0"> 


<tr> 
<td align="center"><strong>Id</strong></td> 
<td align="center"><strong>Nom</strong></td> 
<td align="center"><strong>Classe</strong></td> 
<td align="center"><strong>eau</strong></td> 
<td align="center"><strong>nourriture</strong></td> 
<td align="center"><strong>couleur</strong></td> 
<td align="center"><strong>taille</strong></td> 
<td align="center"><strong>vie</strong></td> 
<td align="center"><strong>dateacqui</strong></td> 
<td align="center"><strong>modifier</strong></td> 

</tr> 
<?php 
while($rows=mysql_fetch_array($result)) 
{ 
?> 

<tr> 
<td align="center"><input type="hidden" name="id<? echo $rows['id']; ?>" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td> 
<td align="center"><input name="nom<? echo $rows['id']; ?>" type="text" id="nom" value="<? echo $rows['nom']; ?>"></td> 
<td align="center"><input name="classe<? echo $rows['id']; ?>" type="text" id="classe" value="<? echo $rows['classe']; ?>"></td> 
<td align="center"><input name="eau<? echo $rows['id']; ?>" type="text" id="eau" value="<? echo $rows['eau']; ?>"></td> 
<td align="center"><input name="nourriture<? echo $rows['id']; ?>" type="text" id="nourriture" value="<? echo $rows['nourriture']; ?>"></td> 
<td align="center"><input name="couleur<? echo $rows['id']; ?>" type="text" id="couleur" value="<? echo $rows['couleur']; ?>"></td> 
<td align="center"><input name="taille<? echo $rows['id']; ?>" type="text" id="taille" value="<? echo $rows['taille']; ?>"></td> 
<td align="center"><input name="vie<? echo $rows['id']; ?>" type="text" id="vie" value="<? echo $rows['vie']; ?>"></td> 
<td align="center"><input name="dateacqui<? echo $rows['id']; ?>" type="text" id="dateacqui" value="<? echo $rows['dateacqui']; ?>"></td> 
<td align="center"><input name="modifier[]>" type="checkbox" id="modifier[]" value="<? echo $rows['id']; ?>" ></td> 
</tr> 
<?php if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox'];}?> 
<?php 
} 
?> 
<tr> 
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> 
</tr> 
</table> 
</td> 
</tr> 
</form> 
</table> 
<?php 
// Check if button name "Submit" is active, do this 
if($Submit) 
{ 
foreach($_POST['id'] as $id) 
{ 


$sql1="UPDATE poisson SET nom='".$_POST["nom".$id]."', classe='".$_POST["classe".$id]."', eau='".$_POST["eau".$id]."',nourriture='".$_POST["nourriture".$id]."',couleur='".$_POST["couleur".$id]."',taille='".$_POST["taille".$id]."',vie='".$_POST["vie".$id]."',dateacqui='".$_POST["dateacqui".$id]."' WHERE id='".$id."'"; 
$result1=mysql_query($sql1); 
} 
} 


mysql_close(); 
?> 
</body> 
</html> 

回答

0

首先我建议以存储后的数据为清楚起见:) 变量然后尝试没有将其存储在运行查询,即:

mysql_query("UPDATE table SET nom='variable1', classe='variable2' WHERE id='condition'; 

等等,就像你没有。您还需要 '检查' 的属性,即checked="yes"

0

复选框是基于checkedHTML attribute检查与否,不是value属性:

此:

<input type="checkbox" checked="checked" /> 

不是这个:

<input type="checkbox" value="1" /> 

该值是与表单一起提交时的内容,例如,您将ab组unch复选框通过使用相同的name属性。