![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| 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: * 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 |
|
|||||||
| Web Design & Programming Discussion of web design, and server-side & client-side scripting |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#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());
Thank you Last edited by xres; 04-06-2009 at 02:48 PM. Reason: Code Tags |
|
|
|
| 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 |
|
|
#2 (permalink) |
|
Design Team Member
|
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 | |
|
|
|
|
|
#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());
|
|
|
|
|
|
#4 (permalink) |
|
Registered User
|
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. |
|
|
|
|
|
#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";
}
Cheers |
|
|
|
![]() |
| Thread Tools | |
|
|