我正在尝试让我的php表单使用多个复选框向db提交数据,并在单独的页面上将其调回以进行编辑。我已经尝试了很多这里列出的方法,由于某些原因无法使代码正常工作
//editmember.php
<?php
include "dbinfo.php";
$id=$_GET['id'];
$query = "SELECT * FROM members WHERE id='$id'";
$result = mysql_query($query);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$record = mysql_fetch_array($result);
?>
<form id="form1" name="form1" method="post" action="updatemember.php">
<label for="rank">Rank</label><select name="rank" id="rank" value="<?php echo $record ['rank'];?>">
<option value="Rank 1">Rank 1</option>
<option value="Rank 2">Rank 2</option>
<option value="Rank 3">Rank 3</option>
<option value="Rank 4">Rank 4</option>
<option value="Rank 5">Rank 5</option>
</select>
<br class="clear" />
<?php
$query2 = "SELECT division FROM members WHERE id='$id'";
$result2 = mysql_query($query2);
while($row = mysql_fetch_array($result2))
{
$div=explode(",",$row['division']);
?>
<label for="division">Division</label>
<input type="checkbox" name="division[]" value="Division 1" <?php if(in_array("Division 1",$div)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="division[]" value="Division 2" <?php if(in_array("Division 2",$div)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="division[]" value="Division 3" <?php if(in_array("Division 3",$div)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="division[]" value="Division 4" <?php if(in_array("Division 4",$div)) { ?> checked="checked" <?php } ?> >
<?php
}
?>
<br class="clear" />
<label for="bio">Bio</label><textarea name="bio" id="bio" cols="45" rows="5"><?php echo $record ['bio'];?></textarea>
<input type="submit" name="submit" id="submit" value="submit" />
<br class="clear" />
</form>
&#13;
//addmember.php
<?php
if(isset($_POST['submit']))
{
include "dbinfo.php";
$rank = $_POST['rank'];
$division = $_POST['division'];
//INSERT
//Create Checkbox values for division
if(isset($_POST['division']))
{
$strdiv = implode(",", $_POST['division']);
}
else{$strdiv= "";}
//
$query = " INSERT INTO members ( rank, division ) VALUES ( '$rank', '$division' ) ";
$result = mysql_query($query);
if( $result )
{
echo 'Success';
}
else
{
echo 'Query Failed';
}
mysql_close($db);
}
else
{
echo 'not authorized';
}
?>
&#13;
//form.php
<form id="form1" name="form1" method="post" action="addmember.php">
<label for="rank">Rank</label><select name="rank" id="rank">
<option value="Rank 1">Rank 1</option>
<option value="Rank 2">Rank 2</option>
<option value="Rank 3">Rank 3</option>
<option value="Rank 4">Rank 4</option>
<option value="Rank 5">Rank 5</option>
</select>
<br class="clear" />
<label for="division">Divisions</label>
<input type="checkbox" name="division[]" value="1" id="division[]" />Division 1
<input type="checkbox" name="division[]" value="2" id="division[]" />Division 2
<input type="checkbox" name="division[]" value="3" id="division[]" />Division 3
<input type="checkbox" name="division[]" value="4" id="division[]" />Division 4
<br class="clear" />
<input type="submit" name="submit" id="submit" value="submit" />
<br class="clear" />
</form>
&#13;
值似乎正确地发布到数据库(根据我的理解),但我无法获取值。
我希望editmember.php上存储的数据显示为已选中/未选中状态,并且能够在需要时更新数据。