Jess McKenzie
Jess McKenzie

Reputation: 8385

Inserting an array into the database

I have am creating a check function that enables me to check that data with the specific id and categoryid is already present in the database:

$fields['Occupation'] = array(3) { [0]=> string(1) "1" [1]=> string(1) "6" [2]=> string(1) "7" }

Line in question:

$occCheck = Jojo::selectQuery("SELECT * FROM {refocus_candidate_category} WHERE canid=? AND categoryid=?", array($emailCheck['id'], $fields['Occupation']));

Why am I getting this Error and how do I resolve:

Unknown column 'Array' in 'where clause'

Full Check Function:

    if($occCheck != FALSE)
    {
        Jojo::updateQuery("UPDATE {refocus_candidate_category} SET canid=?, categoryid=? WHERE canid=? AND categoryid=?", array($emailCheck['id'], $fields['Occupation']));
    }else{
        Jojo::insertQuery("INSERT INTO {refocus_candidate_category} SET canid=?, categoryid=? WHERE canid=? AND categoryid=?", array($emailCheck['id'], $fields['Occupation']));
    }

Upvotes: 0

Views: 1617

Answers (4)

Vaclav Svara
Vaclav Svara

Reputation: 359

It looks $fields['Occupation'] is an array and it's going to be replaced by categoryid=>?< category ID. What about doing it step-by-step for each category make a question to the database?

OR

$occCheck = Jojo::selectQuery("SELECT * FROM {refocus_candidate_category}
                               WHERE canid=".$emailCheck['id']."
                               AND categoryid in
                                 (".implode(",",$fields['Occupation']).")");

if $emailCheck['id'] is not an array

Upvotes: 1

JvdBerg
JvdBerg

Reputation: 21856

You have to make up your mind what Occupation you want to query for:

$occCheck = Jojo::selectQuery("
   SELECT * FROM {refocus_candidate_category} WHERE canid=? AND categoryid=?",
   array($emailCheck['id'], $fields['Occupation'][1]));

In this case i used occupation with index 1, and that will result in occupation 6 selected.

Upvotes: 0

Shades88
Shades88

Reputation: 8360

It's not exactly clear what you are trying to do in your queries, as you are writing these strange values SET canid=?.

But the problem you are facing is, that you are concatinating entire array in your SQL query string. try to echo an array then you will see it getting printed as Array. That's the source of your problem query gets expanded like someField=Array. Now that Array is not even in single quotes, so mysql is assuming it to be a column. Thus your error.

Remove that array($emailCheck['id'], $fields['Occupation']). And just set $emailCheck['id'], $fields['Occupation'][0]. your field['Occupation'] is and entire array so choose some as per your need.

Upvotes: 0

Peter Sobot
Peter Sobot

Reputation: 2557

Is there a specific reason you're passing an Array to the ...Query( functions rather than separate arguments? PHP is passing the one parameter through to the database - which interprets it as a string - so your query is actually searching for the word "Array" instead.

Try this instead:

$occCheck = Jojo::selectQuery("SELECT * FROM {refocus_candidate_category}".
                              " WHERE canid=? AND categoryid=?",
                              $emailCheck['id'], $fields['Occupation']);

Upvotes: 0

Related Questions