Jump to content


Photo

WHERE allowed on INSERTs?


  • Please log in to reply
5 replies to this topic

#1 bcamp1973

bcamp1973
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 11 August 2006 - 06:12 PM

Is this not acceptable?

foreach($_POST['category'] as $value) {
    mysql_query("INSERT INTO table (field) VALUES ($value) WHERE $value NOT IN ($old)");
}

in this case $old is 1 or more values separated by commas...

I'm getting the following error...

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 25 NOT IN (25))' at line 1


#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 11 August 2006 - 06:15 PM

INSERT INTO table (field) VALUES ($value) WHERE $value1 NOT IN ($old));
after old you have an extra ) that doesn't seem to be needed, and I am not familiar with using NOT IN, but either way that ) at the very end should be removed.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 bcamp1973

bcamp1973
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 11 August 2006 - 06:21 PM

actually, that's a typo when i posted the message, but good catch!  i wish that was the problem tho...

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 August 2006 - 06:42 PM

Well with the WHERE Clasuse I'm sure you are supposed to have a column name and not a value from a column.
Like:
INSERT INTO table (field) VALUES ($value) WHERE col_name=$value1
You cannot use a column value on its own.

i'm moving this to the MySQL Help forum.

#5 bcamp1973

bcamp1973
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 11 August 2006 - 10:59 PM

ok, i think maybe i'm not getting the proper use of IN/NOT IN.  I thought (in this case) i could use it to check against a list of already entered values (which have been pulled from a previous query) to make duplicates aren't entered in the database.  Basically i have a page with a bunch of checkboxes on it. if records exist in the database taht correspond to a checkbox then it's check. on submission, i don't want that to be inserted again as a duplicate record?  does that make any sense? :(

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 12 August 2006 - 05:39 AM

Well, if you have an appropriate unique key index, you can use INSERT IGNORE.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users