I have a multiple select drop down box that produces me an array like this:
array(3) { [0]=> string(1) "1" [1]=> string(1) "6" [2]=> string(1) "7" }
What I cannot work out is how to get the multiple select drop down box to insert update and select from or into the database. I seem to only be getting one value
My current code situation is this -> And what the above var_dump
is of:
SELECT * FROM {table} WHERE canid=? AND categoryid=?", array($emailCheck['id'], $fields['Occupation'][0]));
Update Statement: How could I implode this?
UPDATE {table} SET canid=?, categoryid=? WHERE canid=? AND categoryid=?", array($emailCheck['id'], implode(',', $fields['Occupation']));
My DB Structure is:
if I understood correctly and $fields['Occupation']
contains multiple selected values from dropdown, then use AND categoryid IN (?)
and use implode to merge array into string like this: implode(',', $fields['Occupation'])
The complete code will look like this:
myFunction("SELECT * FROM {table} WHERE canid=? AND categoryid IN (?)", array($emailCheck['id'], implode(',', $fields['Occupation'])));
UPDATE
I think in this case, when you need to change these columns for each row independently, it would be better to loop over array in PHP and make separate queries:
for($i=0; $i<count($fields['Occupation']); $i++) {
$id = $fields['Occupation'][$i];
// here make query like before
myFunction("UPDATE {table} SET canid=?, categoryid=? WHERE canid=? AND categoryid=?", array($emailCheck['id'], $id));
}
you have to dynamically generate the parameter placeholder for each value
$params = array_merge(array($emailCheck['id']), $fields['Occupation']);
$catPlaceholders = '?'.str_repeat(',?',count($fields['Occupation'])-1);
myFunction("SELECT * FROM {table} WHERE canid=? AND categoryid IN ("
.$catPlaceholders.")", $params);
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.