将复选框值输入mysql

时间:2011-08-27 18:47:25

标签: php mysql checkbox

我有以下复选框

<input type="checkbox" name="weekday[]" value="Monday" /> Monday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="weekday[]" value="Tuesday" /> Tuesday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="weekday[]" value="Wednesday" /> Wednesday </br>
<input type="checkbox" name="weekday[]" value="Thursday" /> Thursday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="weekday[]" value="Friday" /> Friday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="weekday[]" value="Saturday" /> Saturday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="weekday[]" value="Sunday" /> Sunday &nbsp;&nbsp;&nbsp;

我想将所有选中的值输入mysql的“day”字段,以逗号分隔,

请帮助

5 个答案:

答案 0 :(得分:2)

怎么样

implode(',',$_POST['weekday'])

答案 1 :(得分:0)

要进行搜索,您应该implode(',' , $_POST['weekday'])并使用

$sql = "select * from table where day in ('" . implode("','" , $_POST['weekday']) . "')"; 

在sql查询中

答案 2 :(得分:0)

你将获得$ _POST ['weekday']作为数组。你可以像

一样使用它
$_POST['weekday'][0];
$_POST['weekday'][1];
$_POST['weekday'][2];
$_POST['weekday'][3];
$_POST['weekday'][4];
$_POST['weekday'][5];
$_POST['weekday'][6];

答案 3 :(得分:0)

   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;

   }

修改 这是为了回应关于发布$ days变量的查询的评论,因为我发现很难在评论中格式化代码。

  $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_t‌​ime."','pending')"‌​)or die('Error: There was error while submitting the schedule, please try again.');

答案 4 :(得分:-1)

你可以这样做:

$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);

现在您可以使用$checkbox_str保存在数据库中。