0

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:

enter image description here

2
  • This depends on your database schema. I suggest you post that. Commented Sep 21, 2012 at 0:02
  • @JasonMcCreary Done but still unsure about the update/insert statements Commented Sep 21, 2012 at 0:34

2 Answers 2

1

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));
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, How would I do my update/insert statements? I have updated my question with my update statement, I am getting d.pr/i/Y5y7
it depends what you want to do in update/insert statement, I mean what values should be set to canid=?, categoryid=?. sorry for late reply
No problem im not well so hence why I am struggling. canid = given by the database this works fine but all I am doing is basically saying if the cat id is found with the user can id then do no reenter this information but update it. If the can id was not found then insert. I need one canid catid per row
sorry, I was at work... if you have to change canid and catid per row, I think better would be to loop over array and make separate queries
wait can we just do the insert statement but I need to check if there is no double ups - from previous entries with that id
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.