Try this
first of all you have a change of
$tmpIds = $db->quote($cId);
To
$tmpIds[] = $db->quote($cId);
after you want to use a implode()
$courseID1 = explode($tmpIds, ",");
To
$courseID1 = implode(',',$tmpIds);
Question
I'm new to joomla jdatabase. Currently I having a error code on my query in joomla which I using sourcerer plugin to insert the code.
Here is my code:
$campusID = $_POST['campusID'];
$courseID = $_POST['courseID'];
// Get default database object
$db =JFactory::getDBO();
// Get a new JDatabaseQuery object
$query = $db->getQuery(true);
$tmpIds = array();
foreach($courseID as $cId){
$tmpIds = $db->quote($cId); //sanitize the input
}
$courseID1 = explode($tmpIds, ",");
// Build the query
$query->select($db->quoteName('courseid'));
$query->from($db->quoteName('intake'));
$query->where($db->quoteName('campusid').'='. $db->quote($campusID));
$query->where($db->quoteName('courseid').'IN('.$courseID1.')');
// Set the query for the DB oject to execute
$db->setQuery($query);
// Get the DB object to load the results as a list of objects
$results = $db->loadObjectList();
if($results){
echo 'Good';
}
else{ echo 'Error';}
Apparently, the courseID
is an array that post by other form and it was using a check box. But somehow maybe my logic thinking is bad, I couldn't found anyway to compare the courseID
with the database courseID
to determine whether the courseID
is existing in the database or not, else it might just prompt the error message.
Here is what error message I get from joomla when I am trying to execute it.
Error 1054. Unknown column 'Array' in 'where clause' SQL=SELECT `courseid` FROM `intake` WHERE `campusid`='Campus2' AND `courseid`IN(Array)
Solution
Try this
first of all you have a change of
$tmpIds = $db->quote($cId);
To
$tmpIds[] = $db->quote($cId);
after you want to use a implode()
$courseID1 = explode($tmpIds, ",");
To
$courseID1 = implode(',',$tmpIds);