Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Design Forum > Web Design & Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Web Design & Programming Discussion of web design, and server-side & client-side scripting

Reply
 
LinkBack Thread Tools
Old 04-06-2009, 02:47 PM   #1 (permalink)
Registered User
 
Join Date: Apr 2009
Posts: 3
OS: vista


php SQL INSERT INTO WHERE NOT EXISTS

Please can someone help me with an issue I am having with the INSERT INTO WHERE NOT EXISTS Function. Here is my code so far:

Code:
$query = ("INSERT INTO document_association2 (name) VALUES ('$groupname') WHERE NOT EXISTS (document_association2.name = '$groupname'");
$result = mysql_query($query)
or die("error: ".
mysql_error());
I am wishing to insert a new row into document_association2 but only if if an item in the column 'name' is not already existing.

Thank you

Last edited by xres; 04-06-2009 at 02:48 PM. Reason: Code Tags
xres is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 04-06-2009, 03:41 PM   #2 (permalink)
Design Team Member
 
Redcore's Avatar
 
Join Date: Aug 2007
Location: Jamestown, CA
Posts: 717
OS: Linux Mint 7

My System

Re: php SQL INSERT INTO WHERE NOT EXISTS

Shouldn't that be a subquery? Instead of "(document_association2.name = '$groupname'")" shouldn't it be something more like "(SELECT * FROM document_association WHERE document_association2.name = '$groupname'") ?
__________________
Free Resources
PC Protection - Comodo Firewall | AVG Anti-Virus | WinPatrol | Ad-Aware | Spybot S&D | SpywareBlaster |
Web Design/Programming - KompoZer
(Editor) | Paint.NET (Graphic) | GIMP+GIMPShop (Graphic) | FileZilla (FTP Client) | Free Hosting |
Redcore is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 04-07-2009, 06:13 AM   #3 (permalink)
Registered User
 
Join Date: Apr 2009
Posts: 3
OS: vista


Re: php SQL INSERT INTO WHERE NOT EXISTS

Thank you for your reply Redcore and I understand your logic behind that response. Unfortunately my query still seems to be failing using your suggestion of:

Code:
$query = ("INSERT INTO document_association2 (name) VALUES ('$groupname') WHERE NOT EXISTS (SELECT * FROM document_association2 WHERE document_association2.name = '$groupname'");
$result = mysql_query($query)
or die("error: ".
mysql_error());
Are there anyother means that I could use that will help me simply search the 'name' column, determine if the name has already been entered, then add OR update the row depending on the result. Thanks in advance
xres is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 04-08-2009, 08:49 PM   #4 (permalink)
Registered User
 
maretard's Avatar
 
Join Date: Dec 2007
Location: Georgia
Posts: 74
OS: Windows XP Home Edition, SP3

My System

Send a message via AIM to maretard Send a message via Skype™ to maretard
Re: php SQL INSERT INTO WHERE NOT EXISTS

Note: The OP's question has been answered on Yahoo Answers (though apparently he has not checked yet). :P

Available at: http://answers.yahoo.com/question/in...6133733AAyLnwh

In fact, the OP has posted this on several forums (nothing wrong with that, just found it funny that the first ten results on Google for "INSERT WHERE NOT EXISTS" were the exact same question :P).

To the OP: I found an excellent little blurb on codesplatter for exactly this situation. It allows you to insert a new row into a table if its key is not already present in the table, and update existing rows if the key is already present. Check it out here: http://codespatter.com/2008/04/30/sw...en-key-exists/

Regards,
Alvin

PS: Btw, Redcore, sweet icon, with the reflection and whatnot.
__________________
I'm ridin' dirty.
Oh baby, I'm yellow and nerdy.

Last edited by maretard; 04-08-2009 at 08:50 PM.
maretard is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 04-09-2009, 07:23 AM   #5 (permalink)
Registered User
 
Join Date: Apr 2009
Posts: 3
OS: vista


Re: php SQL INSERT INTO WHERE NOT EXISTS

Thank you all for your help. I have decided on this occasion to use a solution provided by a user of yahoo answers:

Code:
$query = mysql_query("SELECT name FROM database_table WHERE name='$groupname'");
$num_query = mysql_num_rows($query);

if($num_query == "0")**
$_insert = mysql_query("INSERT INTO databasetable_name (name) VALUES('$groupname')");
}
else{
echo "There is a group there already";
}
I think I was making my query more difficult than it needed to be.
Cheers
xres is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 10:05 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85