The html checkbox before query, I chose option 1 and option 4
the data where the values should be stored, but it stores into option 1 and option 2 columns instead while it should store it inside both 'opt1' and 'opt4'
PHP
if( isset( $_POST['picked'] ) )
{
$keepdata = array();
$keepdata = $_POST['pick'];
$sqlinsert = "INSERT INTO traincheckbox VALUES ( null, '$keepdata[0]', '$keepdata[1]', '$keepdata[2]', '$keepdata[3]', '$keepdata[4]' )";
mysqli_query( $sqlnrg, $sqlinsert );
header( "Location: pick.php" );
}
HTML
<html>
<form method="post">
<input type="checkbox" name="pick[]" value="PAID"><label>Activity 1</label></input>
<input type="checkbox" name="pick[]" value="PAID"><label>Activity 2</label></input>
<input type="checkbox" name="pick[]" value="PAID"><label>Activity 3</label></input>
<input type="checkbox" name="pick[]" value="PAID"><label>Activity 4</label></input>
<input type="checkbox" name="pick[]" value="PAID"><label>Activity 5</label></input>
<button type="submit" name="picked">Pick activities</button>
</form>
</html>
Using name="pick[]" causes only to index checked checkboxes from 0 to n.
If you need explicit indexes, then you should force them:
<input type="checkbox" name="pick[0]" value="PAID"><label>Activity 1</label></input>
and so on...
You should remove $keepdata = array();
line from your code. It does nothing, because then it is initialized by $_POST['pick']
. Also have on mind, that values not marked as checked will not be sent in any form. There will be errors because of non-existent keys in array (if you check 1 and 4, then 2 and 3 will not be set). You need to handle this situation.