在mysql中插入多个复选框值

时间:2013-02-28 01:14:32

标签: php joomla

我有一个joomla组件用于约会,我有约会开始约会的日期...我的问题是,我一次只能做一个约会,我希望能够检查多个框所以值对于那些盒子可以保存在mysql中,当我检查多个复选框时,只有最后一个被保存在数据库中...

这里是来自joomla组件的代码,我认为必须进行调整,以便帮助人们,如果你可以......

这是复选框的代码......

$timetableHTML .= '<td class="timeSlot timeFree" ><input type="checkbox" name="appointment[]" value="'.$startKey.'" onclick="changeTimes(\''.$calendar->min_duration.'\',\''.$startKey.'\',\''.$endKey.'\')"/></td>';

这是组件控制器中的保存功能......

function save() {
    global $app;
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $db =& JFactory::getDBO();
    $row =& JTable::getInstance('appointments', 'Table');
    $post   = JRequest::get( 'post',4 );
    if (!$row->bind( $post )) { JError::raiseError(500, $row->getError() ); }
    for ($i=1;$i<=10;$i++) {
        if (is_array($row->{'field'.$i})) $row->{'field'.$i} = implode('|',$row->{'field'.$i}); $row->{'field'.$i} = strip_tags($row->{'field'.$i});
    }
    if (!$row->check()) { JError::raiseError(500, $row->getError() ); }
    if (!$row->store()) { JError::raiseError(500, $row->getError() ); }
    $row->checkin();

            if ($this->config->emails){
                $this->notifyOwner(array($row->id));
                $this->notifyAppointee(array($row->id));
            }

            $url = JRoute::_('index.php?option=com_jxtcappbook'.(JRequest::getInt( 'pop', 0) ? '&view=complete&tmpl=component' : ''));
    $this->setRedirect($url ,JText::_( 'Termin je zakazan!'.$pop ));
}

我google了一下,我想我需要设置jrequest :: get with array,我是对的吗?

1 个答案:

答案 0 :(得分:0)

假设Joomla> 1.6。由于您使用JRequest相当数量:

$appointments = JRequest::getVar('appointments', null, 'post', 'array');

或更好,因为这将在3.0之后弃用,您可以使用JInput:

$jinput = JFactory::getApplication()->input;
$appointments = $jinput->post->get('appointments', 'null', 'ARRAY');

清理输入并添加到DB:

foreach (array_keys($appointments ) as $element) {
    $myappointments[] = $db->quote($element);
}


// construct query using implode and use comma separator
$myappointments = implode(',', $myappointments );
$db =& JFactory::getDBO();
$query  = $db->getQuery(true);
$query = sprintf('UPDATE $db->quoteName(#__mytable) SET $db->quote(...) WHERE $db->quote(...) IN (%s)', $myappointments );
$db->setQuery($query);
$db->query();

你明白了......

编辑(根据您的评论):

我仍然不知道你想要实现的目标。所以我很难提供方向。我会另外再刺它。我猜你想要从表单中检查的值并将其放入数据库中。

//this pulls out the values in an array of all the things that have been "checked" (selected in the checkbox)
$jinput = JFactory::getApplication()->input;
$appointments = $jinput->post->get('appointments', 'null', 'ARRAY');

//**This is not code you need** I just want to illustrate what you are getting.
//This is looping through the values of the checkboxes to see what you have
for($i=0; $i < count($appointments); $i++){
    echo "Selected " . $appointments[$i];
}

我之前提供的代码向您展示了如何获取值并存储到数据库中。我无法给出DB的说明,因为我不知道数据库结构。