1

I have the following if statement that works fine for an insert but I cannot seem to get it to only update the table with items that are not inserted:

Insert:

        for($i=0; $i<count($fields['Occupation']); $i++) {
            $id = $fields['Occupation'][$i];

           CMS::insertQuery("INSERT INTO {table} SET canid=?, categoryid=?", array($emailCheck['id'], $id));
        }
            echo 'found update';

I have tried the following update line with no luck.

CMS::updateQuery("UPDATE {refocus_candidate_category} SET canid=?, categoryid=? WHERE canid=? AND categoryid=?", array($emailCheck['id'], $id));

Full Statement:

$catParams = array_merge(array($emailCheck['id']), $fields['Occupation']);
$catPlaceholders = '?'.str_repeat(',?',count($fields['Occupation'])-1);

$catCheck = CMS::selectQuery("SELECT * FROM {table} WHERE canid=? AND categoryid IN (".$catPlaceholders.")", $catParams);

if($catCheck != FALSE)
{
    for($i=0; $i<count($fields['Occupation']); $i++) {
        $id = $fields['Occupation'][$i];

        CMS::updateQuery("UPDATE {table} SET canid=?, categoryid=? WHERE canid=? AND categoryid=?", array($emailCheck['id'], $id));
    }
        echo 'found update'l
}else{
    for($i=0; $i<count($fields['Occupation']); $i++) {
        $id = $fields['Occupation'][$i];

        CMS::insertQuery("INSERT INTO {table} SET canid=?, categoryid=?", array($emailCheck['id'], $id));
    }
    echo 'else insert';
}
2
  • post the code you are trying for the update, the full one Commented Sep 21, 2012 at 21:28
  • You have four parameters in your update query but you are providing only two values..? Commented Sep 21, 2012 at 21:36

2 Answers 2

1

My guess you need something like following code:

CMS::updateQuery("UPDATE {refocus_candidate_category} SET canid=?,
                  categoryid=? WHERE canid=? AND categoryid=?",
                  array($emailCheck['id'], $id, $emailCheck['id'], $id)
                );

Basically you need as many array elements as ? in your UPDATE statement

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot, but currently it will not insert any new selected options - keep the current inserted items but update with any new selected items. How could I make it do this
0

Seems like you're two array items short:

CMS::updateQuery("UPDATE {refocus_candidate_category} SET canid=?, categoryid=? WHERE canid=? AND categoryid=?", array(
    emailCheck['id'], $id, ,'canid', 'cat_id number here');
);

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.