简体   繁体   中英

Inserting multiple checkbox values in mysql

i have a joomla component for making appointments and i have checkboxes for starting dates of the appointments...my problem is that i can only make one appointment at a time,i want to be able to check multiple boxes so the values for those boxes can be saved in mysql,when i check multiple checkboxes only the last checked is saved in database...

here is the code from joomla component that i think that has to be adjusted so help guys if you can...

this is the code for checkbox...

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

and this is the save function in controller of the component...

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

i googled a bit and i think i need to set jrequest::get with array,am i right?

Assuming Joomla >1.6. Since you use JRequest a fair amount:

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

or better yet since this will be deprecated post 3.0 you can use JInput:

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

Sanitize input and add to 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();

You get the idea...

EDIT (based on your comment):

I still don't know what you are trying to achieve. So it is hard for me to provide direction. I'll take another stab at it. I'm guessing you want to take the values that are checked from the form and put that into the database.

//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];
}

The code I provided before shows you how to take the values and store into a DB. I can't give instructions on the DB because I don't know the DB structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM