2011-12-23 53 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屬性。