0

I have an assosiative array in PHP which is inserting data into a table

foreach($array as $key => $value){
        $query = "INSERT INTO live_list (file_id, date) SELECT ('$key', '$value') FROM dual WHERE NOT EXISTS (SELECT * FROM live_list WHERE file_id = '$key')";
        mysqli_query($link, $query) or die('Query failed: ' . mysqli_error($link));
    }

However as I have moved from an indexed array to an assosiative array, I can't figure out how to insert this data ONLY if the data in my array does not exist in my table.

Query failed: Operand should contain 1 column(s)

Now I am recieving this error

Any help will be great!

2
  • 1
    I would use mysqli_fetch_assoc( ) to read the data stored in the database and see if it matches with what I intend to insert. If it matches, then don't do the insertion. Commented Mar 11, 2014 at 11:46
  • Ok I need to look into this and see if I can pull it off Commented Mar 11, 2014 at 11:54

2 Answers 2

1

Why the use of a sub-query?

Assuming file_id is a unique field (i.e. no duplicates in the whole table), make it a unique index on the table if it isn't already:

ALTER TABLE live_list
ADD UNIQUE (file_id)

and change your query to

INSERT INTO live_list (file_id, date) 
 VALUES ('$key', '$value') 
 ON DUPLICATE KEY UPDATE date = date;

This means that it will simple "update" the value of date to what it already is if it encounters a duplicate key.

Although you should look to bind your parameters to protect against sql injection.

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

5 Comments

I already had this, but I need to check if the key value exists in my table, if it does I dont want to insert it.
Thanks I had to change it to a unique field to get this working as you said
Glad it worked. Do look into prepared statements/bind parameters as I mentioned or your program could be vulnerable. Regardless it is good practise.
Thanks I will look into this however, I had sorted my values by most recent date, now I have added Unique, it scrambles my file ids in a random order, even though before I insert the records they are sorted using uasort function
It doesn't matter what order you insert your data in the table. MySQL will sort by the first column in a SELECT query unless you specify a ORDER BY column_name ASC/DESC.
0

You could do this:

INSERT INTO live_list (file_id, date) 
  SELECT '$key', '$value'
  FROM dual
  WHERE NOT EXISTS (SELECT * FROM live_list WHERE file_id = '$key')

DUAL is a dummy table. The logic is that if the sub query does not exist, the dummy table returns one row with the desiered input.

5 Comments

I tried this and I got a parse error Parse error: syntax error, unexpected 'mysqli_query' (T_STRING)
$query = "INSERT INTO live_list (file_id, date) SELECT ('$key', '$value') FROM dual WHERE NOT EXISTS (SELECT * FROM live_list WHERE file_id = '$key')"
Ahh no semi colon haha.
Now I have Query failed: Operand should contain 1 column(s)
Maybe you have two identical file_id? Try adding a limit to the sub query: INSERT INTO live_list (file_id, date) SELECT '$key', '$value' FROM dual WHERE NOT EXISTS (SELECT * FROM live_list WHERE file_id = '$key' LIMIT 1)

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.