mercredi 21 octobre 2015

Multiple checkboxes udpate PHP/MYSQL

I am new to php, please excuse my lack of knowledge.

I am trying to get my php form to submit data using multiple checkboxes to db and call them back on a separate page for editing. I have tried many methods listed on here and for some reason can't get the code to work right

//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>
//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';
}

?>
//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>

The values seem to be posting to the db correctly (by my understanding) but I am unable to fetch the values.

I would like for the stored data on the editmember.php to show as checked/unchecked, and be able to update the data if needed.




Aucun commentaire:

Enregistrer un commentaire