UPDATE multiple checkbox value

Me again, hope you don’t mind!
For firstChoice and secondChoice, these values are entered from a form and show up in the column seperated by commas for example first choice could be “Tennis, Netball” and second “Football, Swimming, Badminton”.
I can update the name values but not the checkboxes. Any help? I will post how I inserted them in the first place if it helps

THE EDIT.PHP PAGE
[php]$action = isset( $_POST[‘action’] ) ? $_POST[‘action’] : “”;
if($action == “update”){
try{
$query = “UPDATE table1
SET userID = :userID, name = :name, firstChoice = :firstChoice, secondChoice = :secondChoice
WHERE userID = :userID”;

$stmt = $con->prepare($query);
$stmt->bindParam(':userID', $_POST['userID']);
    $stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':firstChoice', $_POST['firstChoice']);
$stmt->bindParam(':secondChoice', $_POST['secondChoice']);
$stmt->bindParam(':id', $_POST['id']);

$stmt->execute();
echo "Updated!";

}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
try {
$query = “select userID, name, firstChoice, secondChoice from table1 where id = ? limit 0,1”;
$stmt = $con->prepare ($query);

	$stmt->bindParam('1', $_REQUEST['id']);
	$stmt->execute();
	
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
	$userID = $row['userID'];
            $name = $row['name'];
	$firstChoice = $row['firstChoice'];
	$secondChoice = $row['secondChoice'];
	$id = $row['id'];
	
}catch(PDOExcepton $exception) {
	echo "Error: " . $exception->getMessage();
}

?>[/php]

AN EXAMPLE OF THE ARRAY FROM THE INSERT.PHP PAGE
[php]if (isset($_POST[‘firstChoice’]) && is_array($_POST[‘firstChoice’])) {
$firstChoiceCB = join (’, ', $_POST[‘firstChoice’]);
}
else $firstChoiceCB = ‘’;[/php]

Not sure what you mean, are you wanting to show the checkboxes on the form as checked if they are in the DB?

If so, you could do something like this:

[php]<input type=“checkbox” name=“chkFirstChoice” value=“Football”<?php if (strrpos($firstChoice, "Football")>0) { echo " checked";} ?> />
<input type=“checkbox” name=“chkFirstChoice” value=“Tennis”<?php if (strrpos($firstChoice, "Tennis")>0) { echo " checked";} ?> />[/php]

etc.

No, sorry, that is a useful thing to do though and I may implement it. I should of been clearer. There is a page that shows all the rows in the database, with the buttons “edit” and “delete” next to them. Delete works fine. Edit brings up the form again and the user inputs the information again and this new info should update the corresponding row in the database. But only the values that aren’t checkboxes get updated properly, and the checkbox values get updated as NULL

Are the check boxes actually checked on the edit page? If none are the value will be null.

On the edit page the form comes up as blank, nothing is pre-checked, the user has to select the checkboxes again and then they press submit. Is this what you mean?
This is an example of some of the form if it helps.
[php]First choice activity



Tennis
Football[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service