entering checkbox value into mysql
i have the following checkboxes
<input type="checkbox" name="weekday[]" value="Monday" /> Monday
<input type="checkbox" name="weekday[]" value="Tuesday" /> Tuesday
<input type="checkbox" name="weekday[]" value="Wednesday" /> Wednesday </br>
<input type="checkbox" name="weekday[]" value="Thursday" /> Thursday
<input type="checkbox" name="weekday[]" value="Friday" /> Friday
<input type="checkbox" name="weekday[]" value="Saturday" /&开发者_运维问答gt; Saturday
<input type="checkbox" name="weekday[]" value="Sunday" /> Sunday
I would like to enter all the checked values into "day" field in mysql separated by coma,
please help
How about
implode(',',$_POST['weekday'])
?
if(isset($_POST['submit_btn_name']))
{
$days="";
if(isset($_POST['weekday']))
{
foreach($_POST['weekday'] as $id)
{
$days.=$id.",";
}
$days = substr($days, 0, -1);
}
echo $days;
}
EDIT this is in response to the comment about the query to post the $days variable as I found it difficult to format code in the comments.
$sql1=mysql_query("INSERT INTO class (class_id, subject_id, student_id, available_days, available_time, status) VALUES ('".$class_id."','".$subject_id."','".$student_id."','".$days."','".$available_time."','pending')")or die('Error: There was error while submitting the schedule, please try again.');
you will get $_POST['weekday'] as an array. you can use it like
$_POST['weekday'][0];
$_POST['weekday'][1];
$_POST['weekday'][2];
$_POST['weekday'][3];
$_POST['weekday'][4];
$_POST['weekday'][5];
$_POST['weekday'][6];
For searching you should implode(',' , $_POST['weekday'])
and use
$sql = "select * from table where day in ('" . implode("','" , $_POST['weekday']) . "')";
in sql query
You can do like this:
$arr = array();
// check for CHECKED checkboxes
for(var $i = 0; $i > count($_POST['weekday']); $i++){
// if this checkbox is checked
if (isset($_POST['weekday'][$i])) {
$arr[] = $_POST['weekday'][$i];
}
}
// convert to comma separated
$checkbox_str = implode(',', $arr);
Now you can use $checkbox_str
to save in the database.
精彩评论